My intent is to add a class to the header when a div is clicked. I have included the website I’m working with, just to make thing’s easier:
URL – http://itsmontoya.com/work/iM/
I have added a class ‘expanded’ to the header. This is to show how the navigation should look after the button has been pressed. I created a simple Javascript which is supposed to provide an alert when I click the button. I can’t seem to get this to work. Any ideas of what I did wrong?
Thanks!
EDIT – I was able to get the alert to properly work when clicking the button div. I’m very close to having this complete! 🙂 Now I’m getting stuck with the variable not passing correctly.
<script type= "text/javascript">
var expanded = false;
function btnClick(){
alert('The variable expanded is '+expanded);
if(expanded === false) {
document.getElementById("header").className = "expanded";
var expanded = true;
} else {
document.getElementById("header").className.replace(/\bexpanded\b/,'');
var expanded = false;
}
};
</script>
I’m updating the ftp server now 🙂
When using jQuery, you have to bind your events in such a way that the elements have already loaded.
You have:
I think what you want is:
The API documentation is going to be your friend here. First step —
ready().UPDATE
You have this call to jQuery:
But your markup is for the HTML5 element
<header>. In that case your jQuery needs to change to:Where
$jis your jQuery object. More typically you would use$orjQuery.Time to bone up on jQuery Selectors!
UPDATE
Here’s your updated page: