Paul
Paul

Reputation: 11756

How do I get a list value with jquery?

I'm able to get the list ID but not the value...what am I doing wrong?

My Code:

$('#listParams ').append('<li id="1">Item 1</li>');
$('#listParams ').append('<li id="2">Item 2</li>');


var items = "";
var lis = $("#listParams li");
for(var no=0;no<lis.length;no++){
    if(items.length > 0) {
        items = items + "|";
    }
    //items = items + lis[no].id + '-|-' + lis[no].text; 
    items = items + lis[no].id + '-|-' + lis[no].val;               
}

alert(items);

Upvotes: 0

Views: 134

Answers (3)

David Thomas
David Thomas

Reputation: 253485

One easier approach is to use map():

var itemsAndValues =    $('#listParams li').map(
                                function(){
                                    return this.id + ':' + $(this).text();
                                }).get().join(',');

JS Fiddle demo.

Upvotes: 0

Shai Mishali
Shai Mishali

Reputation: 9402

Its an <li> element so you need to get the innerHTML , not the value.

 alert($("li#1").html());

Upvotes: 0

Alex K.
Alex K.

Reputation: 175956

How about

var items = [];
$("#listParams li").each(function() {
      items.push(this.id + "-|-" + $(this).text());
});
alert(items.join());

Upvotes: 1

Related Questions