views:

133

answers:

1

This code attempts to dynamically switch the class of the StateContainer div from StateOne to StateTwo to alternate the visibility of the DIV.

When I run it, I always see the following both before and after clicking the button.

Visible first
Visible first
Visible first
Visible first

Would appreciate any suggestions for why this code does not work the way I'm expecting.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"&gt;&lt;/script&gt;

<style type="text/css">
.StateOne .InitiallyHidden { display: none; }
.StateTwo .InitiallyVisible { display: none; }
</style>

<script type="text/javascript">
    $(document).ready(function()
    {

    $('.x').click(
       var s = document.GetElementById('StateContainer');
       s.className = (s.className == 'StateOne' ? 'StateTwo' : 'StateOne');
    );

    });
</script>

</head>
<body>

<button class="x">Change StateContainer</button>

<div class="StateOne" id="StateContainer">
   <div class="InitiallyVisible">Visible first</div>
   <div class="InitiallyHidden">Visible second</div>
   <div class="InitiallyVisible">Visible first</div>
   <div class="InitiallyHidden">Visible second</div>
   <div class="InitiallyVisible">Visible first</div>
   <div class="InitiallyHidden">Visible second</div>
   <div class="InitiallyVisible">Visible first</div>
   <div class="InitiallyHidden">Visible second</div>
</div>

</body>
</html>
+2  A: 

It looks like you're not using the click event properly. You need to provide it with a function:

$('.x').click(function() {
       var s = document.getElementById('StateContainer');
       s.className = (s.className == 'StateOne' ? 'StateTwo' : 'StateOne');
});

Also, if you're already using jQuery, the following syntax is much shorter than document.getElementById (which doesn't have a capital G, by the way):

var s = $("#StateContainer")[0];

Steve

Steve Harrison
Thank you . . . Head scratching comes to an end.
Charlie Kotter
Cool. That's a nice shorthand syntax.
Charlie Kotter
Oops. Spoke to soon - I added your shorthand form - var s = $("#StateContainer"); and it stopped working. Are you sure you wrote that right?
Charlie Kotter
Weird. According to the jQuery docs, it should work: http://docs.jquery.com/Selectors/id#id.
Steve Harrison
Oh, perhaps this doesn't *return* the element... perhaps they expect you to use it like "$("#StateContainer").doSomething()"... I'll keep looking quickly through the jQuery docs...
Steve Harrison
Yep. I have a feeling it's just for chained method calls. Interesting. Thanks for the link.
Charlie Kotter
It stops working because doing $('#StateContainer') returns a wrapped set, not the native element you are using. If you refer to it as s[0].className it would work.
Paolo Bergantino
@Paolo Bergantino: Thanks! I've updated my answer accordingly.
Steve Harrison
Cool. Thanks, guys.
Charlie Kotter