1

I am attempting to give my admins a mouseover of the element they are hovering over to give them a clue to what to edit, if anything. Here is my code:

$(document).tooltip(); $(document).on('mouseover','*',function(){ $(this).attr('title',$(this).prop('id')); console.log($(this)); }).on('mouseout',function(){ $(this).attr('title',''); }); 

This works great for elements with an id. What I need to get is a combination of html element and id (if any) on mouseover.

4
  • What do you mean by "combo html element"? Commented Nov 16, 2016 at 4:26
  • Not clear. On mouseover, you're setting the title of the element. But what should happen when there is no id? Commented Nov 16, 2016 at 4:26
  • what I am trying to get is the element (body,div,span, etc.) it will be helpful, but not necessary to have an id on the element Commented Nov 16, 2016 at 4:27
  • i think you want to get the element type name. If so then you can use code like $(this).prop('nodeName') Commented Nov 16, 2016 at 4:29

3 Answers 3

1

If I understood you correctly, when you hover over an element, you want the id of the element as well as the element type (tag name). If that's the case, then you can use the following:

 $(document).on('mouseover','*',function(){ var id = this.id; var elementType = $(this).prop('nodeName'); // will give you element tag name // do something with id and elementType }).on('mouseout',function(){ $(this).attr('title',''); }); 
Sign up to request clarification or add additional context in comments.

2 Comments

thanks to you guys I now know that there actually were other posts that ask the same question. Should I delete my post?
@phpmydev You don't need to delete the post. If you accept the duplicate question, your question can serve as a signpost for others looking for the same answer.
0

You should be able to short that code using this.id instead of $(this)... Also check your console and pick w/e you need from there and use || operator to pick the one that suits you best.

$(document).on('mouseover','*',function(){ $(this).attr('title',(this.id || some_other_things || yet_another_one)); console.log($(this)); console.log(this); }).on('mouseout',function(){ $(this).attr('title',''); }); 

Comments

0

If id is not defined then get the tagName property of the dom element.

$(this).attr('title', this.id || this.tagName); 

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.