JQuery selector on click event

Atnaize

I want to trigger something when I click on a certain class within a div.

I tried this

$("div .event").click(function() {
    alert($( this ).text());
}); 

And

$("div").on("click", $('.event'), function() {
    alert($( this ).text());        
}); 

//Or 
$(".SimpleCalendar .event").click(function() {
    alert($( this ).text());
}); 
//I do not even know what this is supposed to do ...
$(".SimpleCalendar tbody tr td div .event").click(function() {
    alert($( this ).text());
}); 

And many more but still can not figure out why this is not working

My HTML is the following : enter image description here

PeterKA

The div that you're selecting is the one that has the class .event, not a descendant of it. Therefore the correct selector is div.event. Try this:

$("div.event").click(function() {
    alert($( this ).text());
});

Or just:

//Warning: if elements unlike the div also have the event class then stick to 
//the above as the selector is more specific
$(".event").click(function() {
    alert($( this ).text());
}); 

And don't forget that each of these options should be in DOM ready like so:

$(function() {
    $("div.event").click(function() {
        alert($( this ).text());
    }); 
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jquery $(document).on('click', selector, ... ) vs $(selector).on('click',

From Dev

Jquery click event on selector returns child as target

From Dev

Jquery Class selector on click

From Dev

simulating click event of a different selector

From Dev

How to toggle hover event for another selector on click?

From Dev

jQuery .on() event ignores selector

From Dev

selector not working after append inside the .on() click event

From Dev

Jquery on click with selector/data

From Dev

multiple executions when bind click event on * selector

From Dev

jQuery 'on' click not checking for updated selector?

From Dev

jquery :not() selector inside delegated event

From Dev

Bind click event to selector with knockout

From Dev

jQuery selector click function clarification

From Dev

Jquery Class selector on click

From Dev

jquery $(document).on('click', selector, ... ) vs $(selector).on('click',

From Dev

jQuery click event on :data selector

From Dev

simulating click event of a different selector

From Dev

jQuery selector for target event?

From Dev

jquery on click with not selector

From Dev

Not able to access click event with jQuery selector

From Dev

Jquery on click with selector/data

From Dev

Removing ".on" Click Event in jQuery using 'selector;

From Dev

JQuery selector onchange event

From Dev

Bind click event to selector with knockout

From Dev

Jquery Attribute selector click

From Dev

jQuery on(click)/click event not firing

From Dev

jquery selector on click not working

From Dev

Adding click event from jquery .class[data-attribute='value'] selector

From Dev

click event on different child selector with same selector

Related Related

HotTag

Archive