my jquery script wont recognise a class that has been added?
Hi, I can’t make this code work. The last list item gets the new class called triggered, but it doesn’t fire the alert box when I click it.
$(“.trigger”).click(
function(){
$(‘ul#mediaButtons a’).each(
function()
{
$(this).stop().animate({‘marginLeft’…
});
$(“ul#mediaButtons li a:last”).addClass(“triggered”);
});
$(“.triggered”).click(
function(){
alert(“Hello”);
});
- Category: JQuery Questions
Hi, I can’t make this code work. The last list item gets the new class called triggered, but it doesn’t fire the alert box when I click it.
$(“.trigger”).click(
function(){
$(‘ul#mediaButtons a’).each(
function()
{
$(this).stop().animate({‘marginLeft’…
});
$(“ul#mediaButtons li a:last”).addClass(“triggered”);
});
$(“.triggered”).click(
function(){
alert(“Hello”);
});
Other Questions
Login
Search
Recent Comments
- dgdg_dasdad on How does JQUERY DIFFER FROM JAVASCRIPT ? WHAT IS ITJQUERY ?
- Anas Imtiaz on How does JQUERY DIFFER FROM JAVASCRIPT ? WHAT IS ITJQUERY ?
- David D on How does JQUERY DIFFER FROM JAVASCRIPT ? WHAT IS ITJQUERY ?
- Wolfman on How does JQUERY DIFFER FROM JAVASCRIPT ? WHAT IS ITJQUERY ?
- TnT on Having a Jquery PHP problem?


DJMatus23
Posted 10 months ago
When you click on a “trigger”, it adds the “triggered” class to your anchors ok. But when the last 4 lines of the script above are run, the anchor won’t have the “triggered” class. Once you’ve added the “triggered” class, you’ll need to run the last 4 lines of code again, e.g.
$(“.trigger”).click(
function(){
$(‘ul#mediaButtons a’).each(
function()
{
$(this).stop().animate({‘marginLeft’…
});
$(“ul#mediaButtons li a:last”).addClass(“triggered”).click(
function(){
alert(“Hello”);
});
});
$(“.triggered”).click(
function(){
alert(“Hello”);
});
All I’ve done is applied the click to the same objects that get set as “triggered”.
Assuming that you want to do more than just say hello, you could may want to create a separate function and call that from the .click event.