riogrande
riogrande

Reputation: 349

jquery wont add class to html tag

Im trying to toggle a class to html tag when someone click on button. My button has a ID vertical-grid (#vertical-grid) and when i click on button nothing happens?

<script>
$("#vertical-grid").click(function(e){
    $("html").toggleClass("vertical-rhythm-grid");
    e.preventDefault();
});
</script>

This is html of a button

<a id="vertical-grid" class="btn bggreen" href="#" role="button">DOWNLOAD</a>

I don't know if it matters but my HTML tag already has a no-js class

<html class="no-js" lang="">

Upvotes: 2

Views: 70

Answers (3)

Tobias Sch&#228;fer
Tobias Sch&#228;fer

Reputation: 1358

Try the addClassfunction:

<script>
$("#vertical-grid").click(function(e){
    $("html").addClass("vertical-rhythm-grid");
    e.preventDefault();
});
</script>

This should work.

Upvotes: 0

Yatin Khullar
Yatin Khullar

Reputation: 1590

There might be many reasons for it why it is not working.

Like:

  1. You may have used same id twice or more in that page.
  2. Jquery Conflict.
  3. Jquery Library not loaded properly.

To solve these types of problem always try to look once in firebug console. It helps to solve problem very easily.

If you can see your error in your console just comment it here I will try to help you.

Upvotes: 1

Shankar Sengalani
Shankar Sengalani

Reputation: 302

Try this

<script>
$(document).on('click', '#vertical-grid', function(e){
    $("html").toggleClass("vertical-rhythm-grid");
    e.preventDefault();
});
</script>

Upvotes: 0

Related Questions