views:

75

answers:

3

Heya, I've got some code that I think ought to be working (though I really am fumbling around in the dark here) - I'm just trying to dynamically change a class name on a div tag.

I'm trying to change the div class on id "slider" from "visible" to "hidden". Everything else about this code works just fine, but the class won't change. What am I doing wrong?

I've tried checking to see if the browser was correctly evaluating for IE 6 - it is. The code in the first part of the if statement that initializes slider when it's not IE6 works. And if I replace document.getElementById with something else, that code works too. So it must be a problem with the way I'm calling getElementbyId? It's just doing nothing in IE6.

Here's what I've got:

<script type="text/javascript">


$(window).load(function() {
var isIE6 = false;
        if(/MSIE 6/i.test(navigator.userAgent)) {
          isIE6 = true;
        }

    if(!isIE6)
    {

 $('#slider').nivoSlider({
  effect:'fade', 
  slices:1,
  animSpeed:500,
  pauseTime:3000,
  startSlide:0, 
  directionNav:false,
  directionNavHide:true, 
  controlNav:true,
  controlNavThumbs:false,
      controlNavThumbsFromRel:false, 
  controlNavThumbsSearch: '.jpg', 
  controlNavThumbsReplace: '_thumb.jpg', 
  keyboardNav:true, 
  pauseOnHover:true, 
  manualAdvance:false, 
  captionOpacity:0.8
 });
 }
 else 
 {
 document.getElementById('slider').className = "hidden"; 
 }
});
</script>



<div id="slider" class="visible">
  <img src="/img/nivoslider/slide1.jpg" />
  <img src="/img/nivoslider/slide2.jpg" />
  <img src="/img/nivoslider/slide3.jpg"  />
</div><!-- end slider -->
+1  A: 

If you're using jQuery, which it looks like you are, why not do it this way:

$('#slider').addClass('hidden').removeClass('visible')

This also begs the question: why not use $('#slider').toggle() or $('#slider').hide()and $('#slider').show() as appropriate?

Ken Redler
Thanks for taking the time to respond - I super appreciate it. I tried all three. The first did nothing - the classname on slider stayed the same. The second and third both not only hid the slider div, but the rest of the page content as well. Ideas? IE6 / jquery issue?
Jessica
Can we see more of your HTML? Something's amiss.
Ken Redler
After you said that, I looked very carefully through the HTML. The solution was this: use your $('#slider').hide() script. Then I found out that a div that was opened just before the slider div was never closed. Though the slider div was closed correctly, closing the div above it resulted in a correctly hidden jquery slide div. I want to have your code babies, Ken, thanks.
Jessica
@Jessica: Noted.
Ken Redler
A: 

Just a question: How do you know the classname isn't changing? Have you set the CSS styles to match the hidden class.

On top of that, if the only purpose of the class is to change the visibility, why not just change that directly?

document.getElementById('slider').visibility = "hidden";

If all that doesn't work, and since you are already using jQuery, try using

$("#slider").hide();
Macha
Thanks for the input. I know the classname isn't changing, because I load up IE6, the slider div is a) still visible and b) when I look at the source, the class is still showing as visible. I did try to use .hide(), but that strangely hides the content of everything - the slider and all content after the slider in IE6. The slider div is closed correctly. Maybe that points to an additional bug?
Jessica
Is everything in the slider div also closed correctly? And did changing the visibility instead work? Have you defined .hidden in your CSS?
Macha
View source is not dynamic btw. You need a tool like Firebug or the Developer Tools in IE8+/Chrome/Safari to check the current status.
Macha
A: 

If you want to ensure that you are chaning the class so you don't have to do an add/remove and just want to do a replace using jquery you could do:

$('#slider').attr('class', 'hidden');

or

$('#slider').attr('class', 'visible');

This way you can never get out of synch at least, it will always be just one of those values.

Kelsey