2

How should I get the Parent nodeName of a text while I'm on it with the cursor?

<div id="holder" contentEditable="true"> Stackoverflow is the <b>coolest</b> Q&A website in the world. </div> 

And as a result we have:

  • Stackoverflow is the coolest Q&A website in the world.

So if the cursor is on the coolest I would like to get it's parent nodeName which is b

Please no libraries, just pure javascript.

1
  • Are you okay with a jQuery solution? Commented Feb 3, 2011 at 21:33

2 Answers 2

7
if (document.addEventListener) { document.getElementById('holder').addEventListener('mouseover', function (e) { somevar = e.target.nodeName; }, false); } else { document.getElementById('holder').attachEvent('onmouseover', function (e) { somevar = e.srcElement.nodeName; }); } 

EDIT: updated code and example in accordance with question edit and comments.

See example.

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

2 Comments

Nice to know, that IE understands event as a handler function param. I always thought, that window.event is the only way to get event object. +1
@CIRK: change click & onclick with mouseover & onmouseover, and change .tagName to .nodeName and the above should give you what you ask for :)
1
<div id="holder" contentEditable="true"> Stackoverflow is the <b onclick="alert(this.tagName)">coolest</b> Q&A website in the world. </div> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.