I need to be able to click on some text (id="txt") and fire an event which replaces that text with a textbox; currently I have this code:
var myTxt = document.getElementById("txt");
myTxt.addEventListener("click", name1Click, false);
function textclick()
{
var removeMe = document.getElementById("txt");
var root = document.body;
root.removeChild(removeMe);
var addMe = document.createElement("input");
addMe.setAttribute("type","text");
addMe.setAttribute("id","i");
root.appendChild(addMe);
}
This is not working, however, any ideas?
In the code you have posted here, your eventListener is calling
name1Click, but your function is namedtextclick.When you fix that, it seems to work here: http://jsfiddle.net/jfriend00/55mQq/.
You may want to note that
addEventListener()is not supported in older versions of IE (you have to useattachEvent()instead).