views:

2027

answers:

4

I am wanting to use jquery to do a show/hide of a DIV from a text link.

What makes it a little different from the examples I have found of this site so far is I need a generic way of doing it multiple times on 1 page and also able to use it sitewide on anypage.

It would be really nice if I can put the JS in seperate file that is included into the pages, so maybe it can be wrapped into a function?

Can someone help me here? For making it generic it could be where I assign a div that is shown/hidden with an id like id="toggle-hide-1" and just change the numbers in my page to make it a different show/hide area

I could just name the ID using a name that will make the function show/hide a div and to seperate it from other divs that show/hide on a page I could add a number to it.

below is partial code that will do a show/hide of a div on a link click but is not exactly what I need.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt; 
<script type="text/javascript" >
$(function() { 
    $(".view-code").click(function(evt) {
     $(".tool_block, .view-code").toggle();
    }); 
});
</script>
<a href="#" class="view-code" >view code</a>
<a href="#" class="view-code"  style="display:none">hide code</a> <br  />

<div class="tool_block" style="display:none" >
this stuff is hidden until we choose to show it!
</div>
+1  A: 

If you could wrap the whole collection in a div, it would make it pretty easy.

$(function() {
    $(".view-code").click( function(e) {
        $(this).siblings().andSelf().toggle();
    });
});

<div>
    <a href="#" class="view-code" >view code</a>
    <a href="#" class="view-code"  style="display:none">hide code</a> <br  />

    <div class="tool_block" style="display:none" >
    this stuff is hidden until we choose to show it!
    </div>
</div>

If it doesn't handle the <br />, you could filter the siblings to only div and anchor elements.

tvanfosson
This does require that the anchors and divs be somewhat related in the markup though.
Joel Potter
Only that they be in the same container, but it sounds like that's what he's after.
tvanfosson
I do think this is an elegant solution, if the assumption you have made is true for the OP.
Joel Potter
Only problem is it won't allow me to have multiple sets. This would allow 1 click event for multiple divs but not multiple sets of click events to alter different div's like Joel Potter's answer
jasondavis
@jasondavis -- I'm not sure what you mean. This would apply a handler to every element (in this case anchors) decorated with the "view-code" class. Each click handler would apply only to the elements in the container with the element that the handler is applied to. You could have as many sets of anchor/anchor/div tuples as you wanted as long as they were wrapped in a div so that you could use the siblings() method to select just those divs and anchors associated with the element clicked on.
tvanfosson
+2  A: 

The best approach is probably going to be something using custom attributes. If you markup your anchor with an attribute that tells the jquery which div to toggle, it will be easier to write generic code to do the work.

Something like this:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt; 
<script type="text/javascript" >
$(function() { 
    $(".view-code").click(function(evt) {
        var d = $(this).attr("toolDiv");
        $(".tool_block[toolDiv=" + d + "], .view-code[toolDiv=" + d + "]").toggle();
    }); 
});
</script>
<a href="#" class="view-code" toolDiv="1" >view code</a>
<a href="#" class="view-code" toolDiv="1" style="display:none">hide code</a> <br  />

<div class="tool_block" toolDiv="1" style="display:none" >
this stuff is hidden until we choose to show it!
</div>

Then give each of your anchor-div pairs a unique toolDiv value (doesn't have to be a number).

Joel Potter
that is exactly what I need! 1 thing is when you click a link it loads the # into the URL, do you know how to cancel out that part because it makes the page go to the very top if you are scrolled down any. thanks
jasondavis
Another thing, do you know if it would be possible to wrap this in a function that I could put away in a file elsewhere and then just call it with a 1 liner on the page I need it?
jasondavis
You can stick the contents of that script block into another file, and as long as it is loaded after the jquery library, it will work fine for any page. No need to write any JS on the page directly. To prevent the default behavior of the anchor, just add `return false;` to the end of the click handler.
Joel Potter
A: 

Why not use specific classes instead? Each element you want shown/hidden can have a class called "toggler," as in:

<div class="toggler">
 ...
</div>

You can then toggle the visibility of ALL elements with this class at once, with:

$(".toggler").toggle();

In this scenario, it doesn't make a difference where in the document these elements reside or even what kind of elements they are.

If this functionality needs to be available globally, you can always extend jQuery itself with a custom function, as in:

$.toggleTogglers = function(toggleClass) {
  $("." + toggleClass).toggle();
};

This is nice in that you have flexibility to choose a toggle custom class of your own design.

David Andres
A: 

I like this script and am using it. The issue I am running into is that I have three seperate show hides on the page. I have an $aclss $bclss and $clss...I need them to open independently, which I have done so far without issue. But I need multiple hides at once based on which class is choosen. For example if $aclss $bclss $clss are all shown and I click to hide $bclss, $clss should hide as well. If I click to hide $aclss then $bclss and $clss should hide as well. Basically anything under the class that is clicked needs to close . Does anyone know how I would do this..Thanks

Brad Amster