Search code examples
jqueryhtmlasp.net-mvconmouseover

Get id of element on Mouseover in jquery


Firstly I know this question has very simple answer but first understand my problem. This is my html and script.

<img onmouseover="FacebookprofileTip(1, 0)" onmouseout="FacebookprofileTip(0, 1);" onclick="FacebookprofileTip(0, 1);" id="imgProfile" src="#" />
<div class="row"  id="profile-tip"></div>

Script :

function FacebookprofileTip(post, delay) {
    //How do I get element id here
    //Non-relevant code
}

As per my requirement I want to get the id of "img" tag onmouseover event. but already I am passing 2 arguments to function for post and delay time to display tooltip message. So please tell me how can I get id in jquery.


Solution

  • Its has very simple solution, pass the this reference to element which invoke the event to event handler. Then you can read id property.

    <img onmouseover="FacebookprofileTip(1, 0, this)"  id="imgProfile" src="#" />
    

    Change function as

    function FacebookprofileTip(post, delay, elem) {
         alert(elem.id)
         //Rest of your code, now you can play with elem as required
    }
    

    function FacebookprofileTip(post, delay, elem) {
      alert(elem.id)
    }
    <img onmouseover="FacebookprofileTip(1, 0, this)" id="imgProfile" src="https://lh5.googleusercontent.com/-qQRcAKHdBzE/AAAAAAAAAAI/AAAAAAAAABE/sTjOPZrtj1g/photo.jpg?sz=32" />