Question

I'm fairly new to Jquery, hence the noob questions......

Iv been trying to follow this to toggle a class on and off when a user clicks a link, iv got it now so that if the user clicks a link with lets say id="22" it will tick the tickbox with id="tick_22", what I'm trying to do now is add some user feedback so that the link changes colour when the tick box is ticked. I have tried to follow the example on the Jquery site http://api.jquery.com/toggleClass/ , but for some reason its not working ?. I'm not sure if I need to use (this.class) or just (this) ?.

    $(document).on('click', '.selectlink', function () {
         var myId = $('#check_' + this.id);
         if ($(myId).is(':checked')) {
         $(myId).prop('checked', false);
         $(this).toggleclass( activeselectlink, addOrRemove );
         }
         else {$(myId).prop('checked', true);
         $(this).toggleclass( activeselectlink, addOrRemove );
         }
         countChecked();
    });
    });
    </script>
Was it helpful?

Solution 2

You can put the class to toggle in "class2toggle":

$(this).toggleClass("activeselectlink"); // this should work

OTHER TIPS

check your case, Javascript/jQuery use Camel case..i.e. name it toggleClass and not toggleclass .. http://api.jquery.com/toggleClass/

case is wrong.

Check for it first :

.toggleClass instead of .toggleclass

First its .toggleClass() not .toggleclass().

Second, the variable addOrRemove has not been declared anywhere. It is supposed to be a variable of type boolean.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top