2

This is a follow up to this question:
How to replace DOM element in place using Javascript?

I wasn't the OP but I am facing a similar situation. The initial question was "how to replace an anchor element with a span using Javascript. The answer that was given (thank you Bjorn Tipling ) was to use replaceChild(), with the following example:

<html>
<head>
</head>
<body>
  <div>
    <a id="myAnchor" href="http://www.stackoverflow">StackOverflow</a>
  </div>
<script type="text/JavaScript">
  var myAnchor = document.getElementById("myAnchor");
  var mySpan = document.createElement("span");
  mySpan.innerHTML = "replaced anchor!";
  myAnchor.parentNode.replaceChild(mySpan, myAnchor);
</script>
</body>
</html>

My follow up is: how to add / insert an id (e.g., "xyz")and a function (e.g., onmouseout='doWhatever(this)') to the replaced DOM element?

4
  • 2
    Don't use inline attribute event handlers Commented Mar 27, 2013 at 21:58
  • I'm reading this document. It's doing a good job of explaining the "how", but not the "why", except added functionalities I'm not sure I need at this point. Not trying to pick a fight, I'm just learning and after reading the document I'm still not clear as to the benefit of using the modern approach... Commented Mar 27, 2013 at 22:09
  • Why? - Because :-) Commented Mar 27, 2013 at 22:22
  • Yes, and also stackoverflow.com/questions/6941483/onclick-vs-event-handler . I've added it to my to-do list. Thanks for your patience. ;-) Commented Mar 27, 2013 at 22:24

3 Answers 3

1

You can add an id property by simply setting it:

mySpan.id = "xyz";

And you can attach an event like so:

mySpan.onmouseout = function() {
    doWhatever(this);
}

I would avoid setting event attributes when dynamically creating DOM elements. Assigning the event handlers programmatically is the right way to go.

Sign up to request clarification or add additional context in comments.

2 Comments

Assigning events through HTML attributes is an ancient technique for doing so. The modern, unobtrusive way to hook up event handlers is to do so programmatically. See the link that Bergi posted.
@JDelage basically, instead of <div id="xyz" onclick="doStuff(this)"></div> you want to do var div = document.getElementById("xyz"); div.onclick = function () { doStuff(this); };
1
mySpan.id = "abcd123";
mySpan.onclick = function () { console.log(this.id + " was clicked"); };

Once you have your HTML element cached to a variable, you can just keep using that variable to work with it.

Comments

1

Use this:

<script type="text/JavaScript">
  var myAnchor = document.getElementById("myAnchor");
  var mySpan = document.createElement("span");
  mySpan.appendChild(document.createTextNode("replaced anchor!"));
  mySpan.id = "xyz";
  mySpan.onmouseout = function() {
      doWhatever(this);
  };
  myAnchor.parentNode.replaceChild(mySpan, myAnchor);
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.