Skip to content Skip to sidebar Skip to footer

Cannot Get Event.target Into A String

In order to identify the element on which the user is clicking, I'm trying to use the Event Object target. $(document).click(function (e) { var str = e.target.toString();

Solution 1:

You don't need to convert it to string. Get rid of .toString()

Directly use its ID property.

if (e.target.id == 'myid'){
    //do something
};

However if you want to valdate against selector use .is()

Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.

if ($(e.target).is('.myclass')){
    //do something
};

Solution 2:

In e.traget you have the HTML DOM element. So that, if it has an id, you should be able to do something like

var str = $(e.target).attr("id"); 
if(str.indexOf("id") > -1){ //do something}

Post a Comment for "Cannot Get Event.target Into A String"