tags:

views:

45

answers:

4

Hi, I have a set of div pairs (a 'header' and a 'body'). When the user clicks on the header, the next body section should show/hide (and update the +/- icon). All the divs are written to be "shown" on page load but I'd like to "close" some of them and the user can open them if they wish. Just can't get it to work! Code is below if anyone can help!

Sample section:

  <div class="expandingSection_head" id="expanderAI">
      <img class="imgExpand" src="../images/treeExpand_plus.gif" />
          Header text here
  </div>
  <div class="expandingSection_body">
      body text here
  </div>
  ...more pairs of divs like this on rest of page...

Code to toggle:

    $(".expandingSection_head").toggle(
    function() {
        $(this).css('background-color', '#6BA284');
        $(this).find('.imgExpand').attr('src', '../images/treeExpand_plus.gif');
        $(this).next(".expandingSection_body").slideUp(400);
    },
    function() {
        $(this).css('background-color', '#6BA284');
        $(this).find('.imgExpand').attr('src', '../images/treeExpand_minus.gif');
        $(this).next(".expandingSection_body").slideDown(400);
    });

    $("#expanderAI").toggle();

This last line doesn't "toggle" the specified div (i.e. "close" it). Anyone know why? Probably something simple.

Thanks!

+4  A: 

It won't toggle just by specifying:

$("#expanderAI").toggle();

There should be some event behind it, example:

$('element').click(function(){
  $("#expanderAI").toggle();
});

Update:

Try this:

$('#expanderAI').click(function(){
  $(this).toggle();
});
Sarfraz
false: straight from the docs: The .toggle() method binds a handler for the click event, so the rules outlined for the triggering of click apply here as well.
Patricia
@Patricia: There are two flavors of toggle, see http://api.jquery.com/toggle/#toggle2 and that is what OP is using. Also take a look at the examples for this variation.
Sarfraz
ahhh yes, i mis-read the question. my bad!
Patricia
not sure what you both mean here - sorry. Once the page has loaded, clicking the headers works fine so I think the event is attaching and isn't overriden by my using toggle().
jqwha
is it perhaps because I'm calling it immediately after binding the toggle event? I'm not sure what the difference is between:$("#expanderAI").toggle();and$('element').click(function(){ $("#expanderAI").toggle();});except for the delay incurred waiting for me to click an "element" to trigger the toggle
jqwha
sarfraz: can you do a tiny edit to your quesiton so i can un-down vote it? it won't let me :(
Patricia
@jqwha: Why don't you toggle the element with id `#expanderAI` in your previous toggle function and set the appropriate visibility like you are doing for other elements.
Sarfraz
@Patricia: Sure done thanks
Sarfraz
:) i think changing the last .toggle() to a .click() will solve this. i've done similar things in a project before.
Patricia
@Patricia: Good idea but it won't **toggle** (show/hide), it would simply trigger a click.
Sarfraz
right, but that click would fire the toggle event and hide the corresponding div. unless he's trying to hide that header div. and that seems strange.
Patricia
+1  A: 

the toggle function doesn't appear to have a use with no parameters:

http://api.jquery.com/toggle/

try using $('#elementID').click(); instead

Scott M.
A: 

upon re-reading the quesiton. i'd change this to a .click() then it should fire your toggle handler that were previously attached.

$("#expanderAI").click();

EDIT:

alright: i just wrote a test script: check it out:

<div id="ToggleMe">Toggle Me</div>
<div id="ChangeMe" style="background-color:Aqua;">Hello WOrld</div>
<script type="text/javascript">
 $(function () {
        $('#ToggleMe').toggle(
            function () {
                $('#ChangeMe').css('background-color', '#ff0000');

            },
            function () {
                $('#ChangeMe').css('background-color', '#000000');
            }

        );

        $('#ToggleMe').click();


    });
</script>

so, the div i'm changing SHOULD show as aqua, but on page load it's red. b/c the click() event is firing on the document ready.

so put your $('#ToggleMe').click(); in your document ready section after you attach the handlers and it really should work. the proof is right here!

Patricia
That would simply trigger a click not toggle that is show/hide :)
Sarfraz
I think the answer is halfway between you both :) See my post above. Thanks for all the help so far BTW!!
jqwha
Just wondering if you have any more ideas? :) I don't really want to do a button click :)
jqwha
i do! see the edit.
Patricia
A: 

Just tried a variation on these ideas :). So...

It doesn't work if I just change the last .toggle() to a .click()...but it DOES work if I make that change but call it AFTER the page has loaded. I think there's a timing issue here.

i.e. if I add a button:

and then do:

    $('#tmpbtntog').click(function() {
        $('#expanderAI').click();
    }
    );

then clicking the button toggles the div! However if I just replace the line

$("#expanderAI").toggle();

in my original code above with a .click() it does NOT work! Weird. Any ideas? Can you "delay" these things? I'm calling this in a document.ready, etc. so I don't think that's the prob.

jqwha
@jqwha: See my updated answer please.
Sarfraz