mardi 30 avril 2019

jQuery events don't occur after I change button class

I am trying to change my buttons' classes when being clicked so as to perform different actions the next time they are clicked. (I do not want to use toggle)

After changing the class, the next time these buttons are clicked nothing occurs even though the class has been changed. Can anyone please help me out? I am just starting to pick up jQuery so I don't know a lot of advanced mechanisms.

 $(".showcontactbutton").click(function(){
    $("#contactform").show();
    //$(this).attr('class', 'hidecontactbutton');
    $(this).removeClass('showcontactbutton').addClass('hidecontactbutton');
    $(this).text("Hide Contact Form");
    //html("<button id='hidecontactbutton'>Hide Contact Form</button>");

});

$(".hidecontactbutton").click(function(){
    alert("Works:");
    $("#contactform").hide();
    //$(this).attr('id', 'showcontactbutton');
    $(this).removeClass('hidecontactbutton').addClass('showcontactbutton');
    $(this).text("Contact Us");
});




Aucun commentaire:

Enregistrer un commentaire