Getting element name by position in DOM

I want to find the name or id of the element where I got the top and left position.

I know how to use jquery to find the position of an element (hovering over a div will cause text to appear on screen):

    $(".item").hover(function () {
      var eleoffset = $(this).offset();
      $("#mouse").text("Mouse - Top: " + eleoffset.top + " - Left: " + eleoffset.left);
    });


<div class="item">This is a text block</div>
<div id="mouse">Waiting for position data...</div>

      

0


a source to share


3 answers


I'm not sure what you mean. But you can get the name or id using:

 $(this).attr('id');
 $(this).attr('name');

      



in the hover handler

+3


a source


      $("#mouse").text("Mouse - Top: " + eleoffset.top + " - Left: " + eleoffset.left + " id " + $(this).id);

      



I think,

+1


a source


this.id and this.name should work too. No additional jQuery call required.

0


a source







All Articles