views:

85

answers:

2

I'm trying to achieve something really basic but jquery isn't doing what I expect! I have a div with an id of "stuff". In the css I set the display attribute to "none" with the aim of changing it with jquery. This way the div is hidden unless the user has js enabled. My jquery code is simply

$("#stuff").show();

unfortunately this isn't working and the div remains hidden.

UPDATE: It now seems to be working INTERMITENTLY in Chrome and IE8. Still not in ff.

A: 
$(document).ready(function(){
   $('#stuff').css('display', 'block');
});
jAndy
Which is *basically* exactly what `.show()` is doing.
Felix Kling
This shouldn't be any difference from what he's already doing, it's an instantly animated css change that does exactly this already.
Nick Craver
well, its faster anyway @Felix: its way beyond from doing the exactly same thing
jAndy
@jAndy - Provide one example where `.show()` wouldn't work, but `.css('display','block')` would? I'm having trouble seeing a situation where this would be a fix...
Nick Craver
@jAndy: What is the a big difference? The only difference is that `.show()` will use the previous `display` value if it was not `none`.
Felix Kling
Tried this before i posted question... No joy...
musoNic80
@Felix Kling: .show() must call like a ton of functions to accomplish this actually pretty simple behavior due how jQuery internally handles fx requests
jAndy
@jAndy - It doesn't if you're not giving it a speed, you can see this for yourself... http://github.com/jquery/jquery/blob/master/src/effects.js
Nick Craver
I could swear I recently read an article about this topic (performance of show() and how slow it is even without a speed). Whatsoever
jAndy
+4  A: 

Make sure your code run when the DOM is ready, like this:

$(function() {
  $("#stuff").show();
});

$(function() {}) is short for $(document).ready(function() { });, this runs the code when the DOM is ready, e.g. your div is actually there to find. Otherwise your selector (unless this is at the end of your <body>) probably isn't finding anything. You can see this by putting alert($("#stuff").length); where your code is now, if it alerts 0, this is most likely the problem.

Nick Craver
This is more than likly the issues, your running your function before the actual html content is parsed into the DOM, so it does not exists. by running the function when the DOM is fully loaded more than likely your div will be in scope.
RobertPitt
See above comment... Also, although I haven't tried alert, it does show up as expected as part of the DOM with original CSS rules when I inspect the HTML in firebug.
musoNic80
@musoNic80 - What does the alert show? Ignoring the debugging tips we're giving and only providing one line of code with no markup isn't the best way to find the problem...
Nick Craver
Sorry Nick, I've had to leave my computer for a bit which is why I haven't tried the alert tip. I'm not ignoring suggestions. I'll try it when I can. I have a long jquery file which is working as expected, apart from this one line. This is the only line that tries to target thar div and so I thought that everything would be irrelevant.
musoNic80
Ok, so the alert test resulted in "1". I also tried reversing things. I removed the "display:none" attribute in the css and swapped show() for hide(). Worked as expected. Any other ideas?
musoNic80
What version of jQuery are you using?
Matt
@musoNic80 - I don't have any other solutions off the top of my head...if you had a URL we could see it would be pretty quick to figure out though, but it's just guessing without seeing the rest of it.
Nick Craver
@Nick: Thanks for your help. Unfortunately I'm developing on localhost so don't have a URL to give you.... Are there any other debugging tips you could give me so I can keep trying to figure it out?!?! Or is there someother way to show you the code or recreate the problem?
musoNic80