2

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 6

3

For a pure javascript solution use the innertext property

alert(ar[i].innerText);

Fiddle: http://jsfiddle.net/3fjursaw/

Sign up to request clarification or add additional context in comments.

Comments

1

You need to get the jQuery object in order to use text():

alert($(ar[i]).text());

JSFIDDLE: http://jsfiddle.net/rh6ufn23/1/.

3 Comments

Can you make the jsfiddle link bigger?
@CodyStott No, it's streetstyle stussy.com/us/crown-stripe-tee; everything else would be too big!
haha yeah just a little bit bigger I can't see it! :P Thanks for your solution, wasn't aware i had to use jQuery object to use text(). the innerHTML is great but puts all text in upper-case, i would like to keep my formatting the same and not worry about another step, so your solution is the best for me.
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>

Comments

0

the problem is that you work with the HTMLElement, which is of vanilla js, not jQuery. Try .innerHTML instead of .text()

Comments

0

Use :

var ar = [];
var listItems = $("#sortable li");
listItems.each(function(li) {
    ar.push($(this).text());
});

Working here: http://jsfiddle.net/csdtesting/0uddfms6/

Comments

0

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.

1 Comment

There is no need to use replace, using innerText or textContent will give strings that have html tags removed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.