admin管理员组文章数量:1335135
I have a control which allows users to sort the <li>
in whatever order they want. when the form submits I want to grab the text inside the <li>
for each <li>
put into an array, in the order on the form.
<ul id="sortable">
<li class="ui-state-default">
<span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Protective Services
</li>
<li class="ui-state-default">
<span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Engineering Services and Public Works
</li>
</ul>
I am grabbing the <li>
's with:
var ar = [];
ar = document.getElementById("sortable").getElementsByTagName("li");
I then go through the array:
for(i = 0; i < ar.length; i++){
alert(ar[i].text()); //ar[i].anything results in console errors.
}
ar[i] displays [object HTMLLIElement]
for every <li>
available.
if I try to access the .text/.val/id properties inside the items i get a console error. So I'm assuming this has to do with a parsing/conversion issue?
How do I properly create an array that looks like protective services,Engineering Services and Public Works
and NOT like [object HTMLLIElement],[object HTMLLIElement]
? Or how do I access my text information from the <li>
as a [object HTMLLIElement]
?
I have a control which allows users to sort the <li>
in whatever order they want. when the form submits I want to grab the text inside the <li>
for each <li>
put into an array, in the order on the form.
<ul id="sortable">
<li class="ui-state-default">
<span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Protective Services
</li>
<li class="ui-state-default">
<span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Engineering Services and Public Works
</li>
</ul>
I am grabbing the <li>
's with:
var ar = [];
ar = document.getElementById("sortable").getElementsByTagName("li");
I then go through the array:
for(i = 0; i < ar.length; i++){
alert(ar[i].text()); //ar[i].anything results in console errors.
}
ar[i] displays [object HTMLLIElement]
for every <li>
available.
if I try to access the .text/.val/id properties inside the items i get a console error. So I'm assuming this has to do with a parsing/conversion issue?
How do I properly create an array that looks like protective services,Engineering Services and Public Works
and NOT like [object HTMLLIElement],[object HTMLLIElement]
? Or how do I access my text information from the <li>
as a [object HTMLLIElement]
?
6 Answers
Reset to default 3For a pure javascript solution use the innertext property
alert(ar[i].innerText);
Fiddle: http://jsfiddle/3fjursaw/
You need to get the jQuery object in order to use text()
:
alert($(ar[i]).text());
JSFIDDLE: http://jsfiddle/rh6ufn23/1/.
You need to use the properties ar[i].innerText
or ar[i].textContent
on DOM nodes. The method .text()
would be used if you had a jQuery object
var lis = document.getElementById("sortable").getElementsByTagName("li");
var data = [];
for(var i=0; i<lis.length; i++){
data.push(lis[i].innerText);
}
var jsonFormated = JSON.stringify(data);
document.getElementById("log").innerHTML = jsonFormated;
<ul id="sortable">
<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Protective Services</li>
<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Engineering Services and Public Works</li>
</ul>
<div id="log"></div>
the problem is that you work with the HTMLElement, which is of vanilla js, not jQuery. Try .innerHTML
instead of .text()
Use :
var ar = [];
var listItems = $("#sortable li");
listItems.each(function(li) {
ar.push($(this).text());
});
Working here: http://jsfiddle/csdtesting/0uddfms6/
jQuery solution:
$('li', '#sortable').each(function(){
alert($(this).text());
});
Or to fix your solution in pure JS:
var ar = [];
ar = document.getElementById("sortable").getElementsByTagName("li");
for (i = 0; i < ar.length; i++) {
alert(ar[i].innerText);
}
See fiddle.
本文标签: javascriptHow to create an array of values from LI inside a ULStack Overflow
版权声明:本文标题:javascript - How to create an array of values from LI inside a UL - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742343085a2456992.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论