user1404623
user1404623

Reputation:

Jquery : Getting the attribute value

I need to get the list of all attributes value of an element by using its id. My code is as follows:

<div id="DivRoles" align="center" style="">
<select id="newOptions" name="UserRole" multiple="multiple">
    <option selected="selected" value="1">Normal User</option>
    <option selected="selected" value="5">Senior Manager</option>
</select>
<!-----Need to get values from this list below------->
<div id="ms-UserRole" class="ms-container">

    <div class="ms-selection">
        <div id="currentroles" class="custom-header">Current Roles</div>
        <ul class="ms-list">
            <li class="ms-elem-selected" ms-value="1">Normal User</li><!--Need this value--->
            <li class="ms-elem-selected" ms-value="5">Senior Manager</li><!--Need this value--->
        </ul>
    </div>
</div>

<input type="button" class="input-rounded-submit" value="Save" onclick = "Javascript:UpdateRoles()"/></div>

Here I need to get the values of "Normal User" i.e. "1" and "Senior Manager" as "5". Then I need to concatenate the values and display the final answer as "15".

I m trying the below code but it dosent seem to be working:

function UpdateRoles() {
    alert("Hi");
    var list;

    $.each()('#ms-UserRole', function(){
        list += $.attr(".ms-selection ms-list li ms-elem-selected ms-value").val();
    });

    alert(list);
}

Please help me if any one knows the solution. Thanks, NC

Upvotes: 0

Views: 189

Answers (4)

Travesty3
Travesty3

Reputation: 14469

Try this:

var list = "";
$(".ms-elem-selected").each(function() { list += $(this).attr("ms-value"); });

If you want the numbers sorted, you should store them as an array first, sort the array, and then join it into a string:

SAMPLE

var arr = [];
$(".ms-elem-selected").each(function() { arr.push($(this).attr("ms-value")); });
arr.sort();
list = arr.join("");

Upvotes: 0

Teena Thomas
Teena Thomas

Reputation: 5239

try this:

    var list = '';
    $('.ms-list').each(function(){
     //alert($(this).text());
    list += $(this).find('.ms-elem-selected').attr('ms-value');
   });

   alert(list);

Upvotes: 0

Cosmin
Cosmin

Reputation: 1490

Try :

$('.ms-list').children().each(function(){
    alert($(this).attr('ms-value'));
});

Upvotes: 0

VVV
VVV

Reputation: 7593

Not sure I quite understand but try this

function UpdateRoles() {
    var output = '';
    $('.ms-elem-selected').each(function() {
        output += $(this).attr('ms-value');
    })


    alert(output);
}

Upvotes: 0

Related Questions