• Recent Questions
  • Popular Questions

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”);
});

    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.

Answer this Question :

You must be logged in to post an answer. Signup Here, it takes 5 seconds :)

Other Questions