2

I have created an expanding div that hides on load and expands when clicked using javascript and jQuery. My problem is that I need to use this feature multiple times on each page (10-30x). Is there a way to call the function to work with multiple Div ids using an array or something of that nature (I am a newbie, my apologies), e.g. a few of my div ids would be eb1, eb2, eb3, eb4. and here is my script that currently works on one id:

   <script type="text/javascript">
        jQuery(document).ready(function() {


        jQuery('#eb1').hide();
        //hides box at the begining
        jQuery("#hide").click(function() {
            jQuery('#eb1').fadeOut(300);
            //jQuery('#the_box').hide();
        });
        jQuery("#show").click(function() {
            jQuery('#eb1').fadeIn(300);
            //jQuery('#the_box').show();
        });

    });
</script>

Any help would be appreciated, even a link to an explanation. Thanks, Travis

7 Answers 7

2

Further to John Conde's answer this is also possible using attribute-starts-with:

jQuery('div[id^="eb"]').hide();

JS Fiddle demo.

It is, of course, easier to just use a particular class-name, and the selector then becomes:

jQuery('.className').hide();

JS Fiddle demo.

References:

Sign up to request clarification or add additional context in comments.

Comments

1

You should be able to do this by separating the ids with a comma:

jQuery('#eb1','#eb2','#eb3').hide();

Comments

0

just type "jquery multiple div show hide" into google:

the first link gives this: Show/Hide Multiple Divs with Jquery

:)

Comments

0

Maybe it is cleaner to add a css class to all the div (or whatever tag you use) elements that should behave like that, then use that class in the selector ?

        jQuery('div.hidable').hide();
        //hides all 'hidable' boxes
        jQuery("#hide").click(function() {
            jQuery('div.hidable').fadeOut(300);
        });
etc...

Comments

0

You could create a function to do that.

let me explain

function ToogleDiv(container){

// simple toogle
$(container).toggle();

// a toogle with some effect
$(container).toggle('slow', function() {
 // add some action    }); }

here's is a Jquery example Toogle Jquery Example

Hope this helps.

Comments

0

You can use Attribute Starts With Selector [name^="value"]

var divs = $('div[id^="eb"]');
$(divs).hide();

$('#show').click(function(){
    $(divs).show();
});

$('#hide').click(function(){
    $(divs).hide();
});

Live example on JSFiddle

Comments

0
function show(var id){
     jQuery(id).fadeIn(300);
}

function hide(var id){
     jQuery(id).fadeOut(300);
}

and then, in your divs:

<a id="hide" onClick="hide('eb1')">hide</a>
<a id="show" onClick="show('eb1')">show</a>
<div id="eb1"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.