Jquery - Create a list of all LI names
Given the following HTML block:
<ul id="taglist">
<li><a name="45" href="">Product 1</a></li>
<li><a name="1146" href="">Product 2</a></li>
<li><a name="13437" href="">Product 3</a></li>
<li><a name="51" href="">Product 4</a></li>
</ul>
Is it possible for JQUERY to return STRING, a single variable with name values:
alert(tagliststring);
Will there be a warning: 45,1146,13437,51
thank
+3
3 answers
You can use the function each:
var names = [];
$('#taglist > li > a').each(function() {
names.push(this.name);
});
var result = names.join(',');
alert(result);
Thus, it nameswill be an array filled with each individual name, and resultwill be a comma separated string that you are looking for.
See the jQuery documentation for more information .
An even smaller way to do this is to use the jQuery function map:
var names = $('#taglist > li > a').map(function() { return this.name; }).get();
var result = names.join(',');
, , .
+7