barrypicker barrypicker - 2 months ago 6
Javascript Question

Using JQuery how to get ID of object passed into function

When using JavaScript I can create a function that takes an object as a parameter...

function doSomething(someObject) {
var id = $(object).attr("id");
}


Assuming I call it like...

$("#myobject").click(function (event) {
doSomething(event.target);
});


... how do I get the id? The .attr returns undefined.

I have searched for this topic and I find many examples where the id is passed to the function, not the object itself. I can't seem to find an example where the object is passed.

Answer

You never used passed argument!

var id = $(someObject).attr("id"); 

Another option could be to use this.id as this in event-handler function refers to element on which event is invoked.

Comments