Thomas
Thomas

Reputation: 5089

Problem with Jquery addClass

I am having trouble implementing the jQuery .addClass() effect.

The script I am trying to use looks like this:

<script>
    $(function(){   
        $('#top_right_size_large').click(function(){
            $('#primary').addClass('large_content');
        });
    });
    </script>

I've looked at the documentation and checked all of my parenthesizes but for whatever reason this doesn't add the .large_content class when the #top_right_size_large anchor is clicked. Any ideas why this wouldn't work the way I expect it to?

Upvotes: 0

Views: 151

Answers (3)

Atticus
Atticus

Reputation: 6720

Include javascript:void(0); in your href to prevent any navigation

<a id="op_right_size_large" href="JAVASCRIPT:VOID(0);">..</a>

Upvotes: 0

Thomas
Thomas

Reputation: 5089

Apparently, by adding the code before the tag, the script wouldn't work. I moved the code below the tag and everything works fine. Thanks for all the suggestions!

Upvotes: 0

Volkan Ceylan
Volkan Ceylan

Reputation: 1496

Just a guess, your anchor has an href and when clicked, it reloads the page, try changing code like below:

<script>
    $(function(){   
        $('#top_right_size_large').click(function(e) {
            e.preventDefault();
            $('#primary').addClass('large_content');
        });
    });
</script>

Upvotes: 1

Related Questions