0

I have a page that includes an unordered list of links. I'm trying to sort that list alphabetically by the link text. My javascript is working in Firefox and Chrome, but not in IE. IE overwrites the original link text with undefined.

My script first finds the ul tag, then gets all the a links by TagName, and puts them into an array lis. Then comes this loop where I take the link text and put it into a second array vals so I can sort them. I think this is where the problem is happening.

for(var i = 0, l = lis.length; i < l; i++)

vals.push(lis[i].text);

As far as I can tell, IE doesn't think that .text exists. I saw something in a different post that suggested changing it to .text(), but that didn't work in any browser.

How can I get IE to sort my links?

Full text of script

1 Answer 1

2

.text is not a standard property that works everywhere. You can use .innerHTML instead like this:

vals.push(lis[i].innerHTML);

Or if you just want text, you can use this:

vals.push(lis[i].textContent || lis[i].innerText);

Some browsers support innerText, some support textContent. This line of code gets whichever is not undefined. You can see which browsers support which here: http://www.quirksmode.org/dom/w3c_html.html.

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

1 Comment

.text() is a jQuery method. Since there is no jQuery here, .innerHTML is what you likely want.

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.