Get elementId from event in jquery

function barvaInfo(event) {

$(document).ready(function(){
var nid = window.event.srcElement.id;
}

this works in IE but not in FF. Can I use jquery for this? I am trying to use the jQuery api event, but then I do not know how to get the id from it.

+3
source share
2 answers

If you are using jQuery, you need to assign a parameter to the event handlers, and then pass an argument to your function for each event.

You can also call it from the context of the element that received the event.

     // some mouseover event handler
$('div').mouseover( function( e ) { 
    barvaInfo.call( this, e )
});

function barvaInfo( event ) {
       // element that originated the event
    var nid = event.target.id;

    // in this function, because we're using .call() to invoke it, 
    //    "this" will reference the element that invoked the handler
}
+4
source

event objectnormalized through jQuery for you and passed to each event handler:

$('someelement').bind('click', function(event) { 
    var nid = this.id; // event.target.id
});

, this dom node . , this.id id . , target, .

patrick dw, this node, . event.target - , , , . . .

+2

Source: https://habr.com/ru/post/1787960/


All Articles