views:

263

answers:

2

I am new to Jquery. My setup is as follows:

I have a series of divs that need to be able to be toggled (display hidden and shown). Each div has an action that can be performed so a unique ID will be necessary to know from which div the event came from.

To toggle the div I have a button for each div (which is not located within the div to be toggled).

Right now without jquery, I use the button's onclick event to pass the ID of the corresponding div to be toggled. Using the unique ID I can do the following:

function toggleFlag(divId) {
    var div = document.getElementById(divId);
    div.style.display = (div.style.display=="block" ? "none" : "block");
}

divId is unique so I also know where the event comes from if action within the div is performed.

the anchor code looks something like this:

onclick="toggleFlag('id-111');"

where the '111' is the unique id

First off, is it even worth it to use jquery for this if the extent of my javascsript isn't much more complicated (aside from maybe simple ajax).

More importantly, how would this properly be done using jquery?

Update: I am very close to solving this. I managed to get it to work using one of the suggestions below requiring unique class names for each button. A couple of the methods suggest implementations where the class name is the same for all buttons (I want this in order to be able to statically assign styles to the button class), but I could not get these to work. Could somebody please elaborate on the solution? Thanks!

+1  A: 

You could do something like this:

HTML:

<button id="btnId1" class="divId1" value="Click me to toggle divId1"/>
<button id="btnId2" class="divId2" value="Click me to toggle divId2"/>
etc...

<div id="divId1">div 1</div>
<div id="divId2">div 2</div>
etc...

SCRIPT:

$(function() {
    $("button").click(function() { 
        var divId = $(this).attr("class");
        $("#" + divId).toggle();
    });
});

This approach has the advantage of only defining the event once for all buttons. You could also use another strategy for storing the div id in the button information, like a non-standard attribute - which jquery can pick up as well, or make the div id a part of the button id like so:

<button id="btn_divId1" value="Click me to toggle divId1"/>
<div id="divId1">div 1</div>

and then extract the id of the div from the id of the button clicked (personally this is the approach I'd take)

Hope this helps

EDIT: To answer the first question, yes you would benefit from doing this with jQuery since it would shorten the amount of code that you are writing and it would allow you to move to unobtrusively assigning events to the buttons which is a good thing :)

EDIT 2: Using non-standard attributes:

HTML:

<button id="btnId1" divid="divId1" class="btnToggle" value="Click me to toggle divId1"/>
<button id="btnId2" divid="divId2" class="btnToggle" value="Click me to toggle divId2"/>
etc...

<div id="divId1">div 1</div>
<div id="divId2">div 2</div>
etc...

SCRIPT:

$(function() {
    $("button").click(function() { 
        var divId = $(this).attr("divid");
        $("#" + divId).toggle();
    });
});
Darko Z
thanks, I think maybe your 2nd approach is what I'd ideally like. as I commented to tvanfosson, I'd like each button (or anchor) to have the same class so that I can statically assign styles to them..your first method requires unique class names..could you maybe elaborate on your second solution as I am very new to jquery? thank you very much
added an edit to demonstrate using non-standard atributes
Darko Z
+1  A: 

Let's say that you build your anchors to have an id that corresponds with the id of the DIV to toggle and further that they all have a common CSS class. For example,

<a href="#" id="a_111" class="toggleButton">Toggle</a>
<div id="d_111">Some stuff.</div>

Now you could use jQuery to build the click handlers for all of these pretty easily.

$(function() {
    $('a.toggleButton').click( function() {
        var divId = $(this).attr('id').replace(/a_/,'d_');
        $(divId).toggle();
    });
});
tvanfosson
I kinda like this approach because it allows me to define a single class for all of the buttons...however I could not seem to get this to work...Darko Z's approach works, but each button has a different class...I will try this again but maybe there is a small error somewhere (I am very new with jquery) thanks
jsguy, in tvanfosson code assumes that your DIV ids and your button/link ids follow certain pattern. E.g. if your link id is a_111, your DIV id must be div_111. If that condition is satisfied, his code will toggle div_111 if you click on the link a_111.
SolutionYogi