Keno Goertz Keno Goertz - 1 year ago 62
HTML Question

How do I add an input form when clicking an HTML link using javascript?

I want to have a

containing a link. When clicked, the link should be replaced by
<input type="text">
, but it just doesn't work.

Any other html code works, but for some reason I can not replace the link with an input:

function working() {
document.getElementById("working").innerHTML = "<h1>Test</h1>";
function notworking() {
document.getElementById("notworking").innerHTML = "<input type="text">";

<div id="working">
<a onclick="working();" href="#">This works!</a>
<div id="notworking">
<a onclick="notworking();" href="#">This doesn't!</a>
Notice that the entire code doesn't work unless you comment out the "notworking()" function!

Answer Source

You need single quotes for your nested quotes;

function notworking() {
document.getElementById("notworking").innerHTML = "<input type='text'>";

I've done this so many times haha, when using quotes inside double quotes always use single quotes.

Hope it helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download