Reputation: 11
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?
Upvotes: 0
Views: 267
Reputation: 707766
.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.
Upvotes: 2