tags:

views:

35

answers:

2
<script type='text/javascript'>
$(document).ready(function() {
  $('div.Syb> div').hide();
  $('div.Syb> h4').click(function() {
   var span = $(this).children('span:first').attr('class');

  //    span.text(span.text()=='+'?'-':'+');


     span = (span == 'plus')?'minus':'plus';

   $(this).children('span:first').attr('class',span);

 $(this).next('div').slideToggle('fast')
 .siblings('div:visible').attr('css','plus').slideUp('fast');

//    $(this).siblings('div.Syb>h4 >span:first').attr('css','plus');

  });
});
</script>

when ever the div gets closed in need to put + symbol back...which i am not able to do ..any help on this?

<div class="Syb">
  <h4><span class='plus'></span>Title 1</h4>
  <div>Lorem...</div>
  <h4><span class='plus'></span>Title 2</h4>
  <div>Ipsum...</div>
  <h4><span class='plus'></span>Title 3</h4>
  <div>Dolor...</div>
</div>

is my html

A: 

span = (span == 'plus')?'minus':'plus';

   $(this).children('span:first').attr('class',span);

instead of this

this is code for changing class i.e toggling class

if ( $('span:first').hasClass('minus') ) {
    $('span:first').removeClass('minus');
   $('span:first').addClass('plus');
} else {
    $('span:first'').addClass('minus');
    $('span:first').removeClass('plus');
}
Pranay Rana
@pranay_stacker - i think you got my question wrong. when an element opens up. i must make all other elements span class to + .. how do i do this
pradeep
but for that you just need to toggle between + and - that you can do it div click fuction easily
Pranay Rana
A: 

You can use .toggleClass(), like this:

$(this).children('span:first').toggleClass('plus minus');

I can't be sure that the selector to find the <span> is right without the HTML though. Since the element has one class or the other, a toggle of both effectively swaps them, short and simple :)

Nick Craver
@Nick Craver - i think you got my question wrong. when an element opens up. i must make all other elements span class to + .. how do i do this.
pradeep
@pradeep - If your structure is what I think it is, try `$(this).siblings('div').children('span:first').addClass('plus').removeClass('minus');`
Nick Craver
@Nick Craver - did not work for me dude.<script type='text/javascript'>$(document).ready(function() { $('div.Syb> div').hide(); $('div.Syb> h4').click(function() { var span = $(this).children('span:first').attr('class'); // span.text(span.text()=='+'?'-':'+');$(this).children('span:first').toggleClass('plus minus');$(this).siblings('div').children('span:first').addClass('plus').removeClass('minus'); $(this).next('div').slideToggle('fast') .siblings('div:visible').slideUp('fast'); });});</script>
pradeep
@pradeep - We *need* to see your HTML, can't tell you how to traverse something if I have no idea what it looks like :)
Nick Craver
yeah i have added html part in the post .please check it.
pradeep
@pradeep - Here's a working version, just change `div` to `h4` in my previous comment: http://jsfiddle.net/n2cZ7/
Nick Craver
@Nick Craver - dude its still not working. see when all divs are closed the color is red rite. when one opens its color is green. try opening the second one when 1st one is still open. colors get jumbled up.
pradeep