Keno Goertz Keno Goertz - 23 days ago 5
HTML Question

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

I want to have a

div
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>
<div id="notworking">
<a onclick="notworking();" href="#">This doesn't!</a>
</div>
<p>
Notice that the entire code doesn't work unless you comment out the "notworking()" function!
</p>




Answer

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!