Javascript addEventListener to add a textbox on a click?

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?

Answers


In the code you have posted here, your eventListener is calling name1Click, but your function is named textclick.

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 use attachEvent() instead).


Need Your Help

ERR Failed to find file(s) for depdency reference

extjs build sencha-touch sencha-touch-2

I have added extra js file Fileup.js for file upload functionality.It works fine,but while building trigger this error.I have put Fileup.js directly under extensions folder.Any help?

Is this possible to get div element position in JQuery

javascript jquery html

Is this possible to get div element position in JQuery?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.