tags:

views:

38

answers:

3

I am unable to get this event to fire:

  $("#about").click(function()
  {  //I have put alert("foo") here, won't fire
    $("#about_stuff").toggle();
  });

snip

   <li ><a href="#a" id="about">About</a>

I've tested the toggle line in Firebug and it successfully works - I am at my wits end, I've checked it against multiple examples and it persistently refuses to work.

A: 

Do you by chance have another element with id="about" assigned to it? If so, the browser may only select the first. Try a few basic diagnostics:

alert($('#about').length);
alert($('#about').eq(0).attr('href'));

If still no joy, do you have a link to the dev site, or is it private?

Joseph Mastey
Nope - just checked. Also, I've renamed it a few times, just to make sure no weirdness crept in.
Paul Nathan
Answers - "1", and "#a". (which is what I would expect, I think...)
Paul Nathan
http://pastebin.com/5hyVrH2d here's a pastebin of a slightly redacted code- it'll die after a while. I'll edit the OP once I get the thingie sorted out.
Paul Nathan
+1  A: 

From your code:
First, you have document.ready

$(document).ready(
  function()
  { // <-- start ready
  $("code").hide();
  }); // <-- end ready

Now, the following code isn't in document.ready, and written before the links have loaded. This is why the selector is empty when this code runs:

  $("#about").click(
  function()
  {  
  $("#about_stuff").toggle();
    return false;
  });
Kobi
I'm not sure I follow - does the selector code have to be assigned during document.ready?
Paul Nathan
Yes, it does. Your `<script>` is written before the `<a>` tag - `#about` doesn't exist yet when the code runs.
Kobi
Interesting! Well, it works. I'll have to rummage up a readable summary of the DOM model. Thanks!
Paul Nathan
Probably a good idea to move your js down the page too... it'll slow down the page load while the javascript executes.
CurtainDog
Oh wow. I just assumed when I read it that it was in the document ready block
Alastair Pitts
A: 
$("#about").click(function(e){  
    e.preventDefault(); // to prevent default event
    $("#about_stuff").toggle();
});
Jenechka