Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm the begginer . I have tried to append span into the list items which i have been dynamically created.But its not working. Within the function this code is working outside of the function its not working. give me the solution pls

What I have tried:

var listcheck=document.getElementsByTagName("LI");
var span=document.createElement("SPAN");
for(var i=0;i<=listcheck.length;i++)
{
var span=document.createElement("SPAN");
var txt = document.createTextNode("\u00D7");
span.className = "close";
span.appendChild(txt);
span.textContent="X";
span.className+="closeicon";
listcheck[i].appendChild(span);
}

function searchinput()
{
	var li=document.createElement("li");
	var inputtext=document.getElementById("client_course").value;
    var t = document.createTextNode(inputtext);
document.getElementById("myUL").appendChild(li);
li.appendChild(t);
}
Posted
Comments
Chris Copeland 25-Mar-21 7:39am    
When you say it's not working outside of the function, does the script produce any errors? Have you tried debugging it within the browser to see what part is failing?

I can see immediately that your for loop is checking i<=listcheck.length, you probably need to do a < check instead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900