April 29, 2011

Hiding and showing divs depending on variable value

Question by user712027

Can anyone please tell me why the code below is not showing the div ticker? it just shows div main ..

Thanks a lot!

<script language="javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script type="text/javascript"> 
    $(document).ready(function(){ 


        $('.ticker').hide();
        $('.main').show();
        $('.other').hide();

    }) 
</script>
<?php

$main = 'ticker';

if ($main=="ticker"){?>
    <script type="text/javascript"> //alert('flag'); //this alert shows ok
        $('.ticker').show();
        $('.main').hide();
        $('.other').hide();
    </script>
    <?php


}

?>
<div class="main">main</div>
<div class="ticker">ticker</div>
<div class="other">other</div>

Answer by Derek

Looks like you’re missing the $(document).ready() from the second script block.

Answer by Starx

Try this

<?php    
$main = 'ticker';    
if ($main=="ticker"){?>
<script type="text/javascript"> //alert('flag'); //this alert shows ok
    $(document).ready(function() {
        $('.ticker').show();
        $('.main').hide();
        $('.other').hide();
    });
</script>
    <?php
}
?>

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!