tags:

views:

39

answers:

1

i have jquery accordion script like this..

<script type='text/javascript'>
$(document).ready(function() {
  $('div.Syb> div').hide();
  $('div.Syb> h4').click(function() {
    $(this).next('div').slideToggle('fast')
    .siblings('div:visible').slideUp('fast');
  });
});
</script>

i need to show + and - symbol before the div ..to show that its expanded and collapsed..

how do i do it.?

+2  A: 

If you're not fussed about Internet Explorer 7 or less, then you can do this in CSS, which is where it should be ideally (as it is presentation, not content):

div.Syb> div:before
{
    content: '+';
}
div.Syb> div.hidden:before
{
    content: '-';
}

Or you could play around with background images.

Then just simply use toggleClass() to give your div a .hidden class.

If you want to do it in straight up jQuery, you'll probably have to change your markup a little:

<div class="Syb">
    <h4><span>+</span>Title One</h4>
    <div id="one">Text</div>
    <h4><span>+</span>Title Two</h4>
    <div id="two">Text</div>
    <h4><span>+</span>Title Three</h4>
    <div id="three">Text</div>
</div>

Then you can simply change your jQuery as such:

<script type='text/javascript'>
$(document).ready(function() {
    $('div.Syb> div').hide();
    $('div.Syb> h4').click(function() {
       var span = $(this).children('span:first');
       span.text(span.text()=='+'?'-':'+');
        $(this).next('div').slideToggle('fast')
            .siblings('div:visible').slideUp('fast');
    });
});
</script>
Keithamus
@Keithamus - thanks dude..that worked for me.. i need to understand the code .. what does children('span:first'); do ?
pradeep
Sure! Children (http://api.jquery.com/children/) searches the children elements (defined as 'span' here) of the element that called it, in this case the H4, so it says "all of the SPAN tags inside the H4 tag". The :first (http://api.jquery.com/first-selector/) is a pseudo selector that simply says "the first in the list of elements", this means you could have multiple SPAN tags inside the H4 and still only the first would be effected. Inside the span.text is a shorthand conditional statement, which you can read up more here: http://www.scribd.com/doc/1026312/Javascript-Shorthand-QuickReference
Keithamus
thanks for the beautiful explanation
pradeep
@Keithamus - i have make 2 images for these + and - symbol . how do i switch b/w those 2.
pradeep