senzacionale
senzacionale

Reputation: 20906

jQuery get id by .event function

var e = jQuery.Event("onmouseover");
e.attr("id");

I want to get id of this element but it does not work. e returns object.

Upvotes: 0

Views: 3503

Answers (1)

David Tang
David Tang

Reputation: 93664

I don't think jQuery.Event, which creates a new event object to be passed around to the elements, does what you want.

I think you only want to get the ID of certain elements when the mouse passes over them? If so, and the elements have a common class name, then do:

$('.someClassName').mouseover() {
   alert(this.id);
});

If they are all of the same tag, e.g. <p>, then do something like:

$('tag').mouseover() {
   alert(this.id);
});

If you want to do this for all elements on the page (not recommended, usually you can narrow it down):

$(document).delegate('*', 'mouseover', function (e) {
   if (this == e.target) alert(this.id);
});

You may want to read more about jQuery selectors to get to the elements that you want.

Upvotes: 1

Related Questions