2

This simple code should change the class of the body element. But it doesn't work!

May the problem be in the wrong jQuery version link?

Besides, pretty strange, but in http://jsfiddle.net/4Bfa7/1/ it seems to work!

<!doctype html>
<html lang="en">

<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>    
<script type="text/javascript">    
    $("li.work").click(function(){
        $('body').removeClass();
        $('body').addClass("work");
});

$("li.pret").click(function(){
        $('body').removeClass();
        $('body').addClass("pret");
});

$("li.port").click(function(){
        $('body').removeClass();
        $('body').addClass("port");
});

$("li.contact").click(function(){
        $('body').removeClass();
        $('body').addClass("contact");
});
</script>    
<style>
body.contact{
background-color: red;}
</style>
</head>    
<body class="front">
<div id=" container">
<header>

<h1 id="site-name"><a href="/"> Crearea web site</a></h1>
<nav>

<ul class="main-nav">

<li class="careers"><a href="/ro" onclick><span class="menu-item-title"> HOME</span></a>
</li>
    <li class="work"><a href="/ro/about"><span class="menu-item-title"> About</span></a>
</li>
<li class="pret"><a href="/ro/preti"><span class="menu-item-title"> Pretul</span></a>
</li>

<li class="port"><a href="/ro/portfolio"><span class="menu-item-title"> Portofoliu</span></a>
</li>
<li class="contact"><a href="#"><span class="menu-item-title"> Contacte</span></a>
</li>

</ul>
</nav>
</header>
</body>
</html>
1
  • 1
    It seems you are missing document ready handler. Commented Sep 8, 2013 at 16:12

4 Answers 4

2

Try it this way:

$( document ).ready( function() {
  /* your event handlers */
});

The scripts in your code get executed before the DOM elements exist, so there isn't anything to bind on. $( document ).ready() gets executed when the DOM is fully loaded.

Sign up to request clarification or add additional context in comments.

Comments

0

wrap your code inside document.ready.

$(function(){
   //
});

Comments

0

Wrap code into $( document ).ready(); or place code at bottom of document directly before tag </body> or put the code in a separate file. This file will be load after loading document.

Comments

0

Try removeAttr when document is loaded. RemoveAttr method additional remove attribute from Your code, removeClass only clean class attribute value.

$(document).ready(function (){  

    $("li.work").click(function(){
            $('body').removeAttr('class');
            $('body').addClass("work");
    });

    $("li.pret").click(function(){
            $('body').removeAttr('class');
            $('body').addClass("pret");
    });

    $("li.port").click(function(){
            $('body').removeAttr('class');
            $('body').addClass("port");
    });

    $("li.contact").click(function(){
            $('body').removeAttr('class');
            $('body').addClass("contact");
    });
}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.