Search code examples
javascripthrefinnerhtmlgetelementbyid

Call javascript function from .innerHTML href


I am altering a P element on a page by calling it's id with a javascript function and I am trying to call another javascript function called closeWindow from the HREF of a .innerHTML like below.

document.getElementById("link").innerHTML = "<p>You may close this window <a href="javascript:closeWindow();">here</a></p>"

However the javascript:closeWindow is not getting called or I should say is not even being read at all and the function fails.

Any suggestions would be appreciated


Solution

  • Your quotes are negating one another, as you can see in the code view. Use single quotes or escape your double quotes:

    document.getElementById("link").innerHTML = "<p>You may close this window <a href='javascript:closeWindow();'>here</a></p>"