/    Sign up×
Community /Pin to ProfileBookmark

ul.insertBefore function of an Unordered List is not working in IE

Hi,
Please see the following part of my javascript code….

var ul =document.createElement(“ul”);
var li =document.createElement(“li”);
var p = document.createTextNode(tem[k]);
li.appendChild(p);
ul.insertBefore(li, ul.getElementsByTagName(“li”)[0];

The ul.insertBefore function is working properly in Firefox but not in IE. Please help out me…..

regards
Ranjan Yeng

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@mrhooMar 13.2008 — You seem to be inserting a list item before an undefined list item, which should return null in any browser...

The first child of an element needs to be appended to the element.

There is no node for it to be [B]before[/B]...

Also, as written your code will throw an error. You have an open parenthesis.
Copy linkTweet thisAlerts:
@ranyengauthorMar 13.2008 — First thing.... I told you it is working properly in firefox...

Which list item I have not defined?

And which parenthesis you are talking about?

Plz see the total code which is working in firefox.....

what I am doing is...

--if an Unordered list exist

then remove it first...

then create a new UL

function change(element){

var sWord=element; // those characters typed in a input box
var words=document.getElementById("words").value; //list of all words
var tem=new Array();
tem=words.split(",");
var numW=tem.length-1;
var k=0;
var div=document.getElementById("di");

if (div != null) {
while (div.hasChildNodes()) {
div.removeChild(div.lastChild);
}
}

var sWL=sWord.length;
var ul =document.createElement("ul");
for(k=0;k < numW; k++){
var tt=tem[k].substring(0, sWL);
if(sWord==tt){
var li =document.createElement("li");
li.id=tem[k];
var p = document.createTextNode(tem[k]);
li.appendChild(p);
ul.setAttribute('style','list-style-type:none;');
ul.insertBefore(li, ul.getElementsByTagName("li")[1])
}
}
div.appendChild(ul);
}
Copy linkTweet thisAlerts:
@mrhooMar 13.2008 — ul.insertBefore(li, ul.getElementsByTagName("li")[1])

Where is this list item you are inserting the first list item before?
Copy linkTweet thisAlerts:
@ranyengauthorMar 13.2008 — I think... since there is no item inserted, the following code will insert the element as a first element of the Unorder List

ul.insertBefore(li, ul.getElementsByTagName("li")[0];


Or, can u give me alternative solution?
Copy linkTweet thisAlerts:
@ranyengauthorMar 13.2008 — OR, is there some other function like ul.insertAt() or ul.appendChild()
Copy linkTweet thisAlerts:
@ranyengauthorMar 13.2008 — Ok.... I got the solution. I can use

ul.appendChild(li);

instead of

ul.insertBefore(li, ul.getElementsByTagName("li")[0];

Thanks marhoo
Copy linkTweet thisAlerts:
@ranyengauthorMar 13.2008 — ul.setAttribute('style','list-style-type:none;');

which set the type of the unorderd list to null is not working in IE. Can you please give me some idea
×

Success!

Help @ranyeng spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...