Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Lets say I have an HTML page, with 5 div's.
In addition I have four buttons.
I'd like to activate every button, so that few divs will appear, but few will be collapsed.
But some of the divs can be opened by few buttons,
for exemple:
button1 : shows div1, div3, div5
button2 : shows div2, div3
button3 : shows div1, div4, div5
button4 : shows all divs.

Can someone help me to do it?
Thanks!

Tali.
Posted

1 solution

Copy this into a new html file, and open with your browser.

HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Button/Div Example Page</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <script type="text/javascript">
        function buttonPressed(button) {

            //hide them all
            $("#div1").hide();
            $("#div2").hide();
            $("#div3").hide();
            $("#div4").hide();
            $("#div5").hide();

            //Set the ones you want to show
            switch (button) {
                case 1:
                    $("#div1").show();
                    $("#div3").show();
                    $("#div5").show();
                    break;

                case 2:
                    $("#div2").show();
                    $("#div3").show();
                    break;

                case 3:
                    $("#div1").show();
                    $("#div4").show();
                    $("#div5").show();
                    break;

                case 4:
                    $("#div1").show();
                    $("#div2").show();
                    $("#div3").show();
                    $("#div4").show();
                    $("#div5").show();
            }

            $("#status").html("You pressed button " + button.toString());
        }
    </script>
</head>
<body>
    <div id="div1">Div 1</div>
    <div id="div2">Div 2</div>
    <div id="div3">Div 3</div>
    <div id="div4">Div 4</div>
    <div id="div5">Div 5</div>
    <div><button onclick="buttonPressed(1)">Button 1</button><button onclick="buttonPressed(2)">Button 2</button><button onclick="buttonPressed(3)">Button 3</button><button onclick="buttonPressed(4)">Button 4</button></div>
    <div id="status">Press a button........</div>

</body>
</html>


Update in response to comments;
To make it work using Links instead of buttons, all you need to do is change the div containing the buttons to;
HTML
<div><a onclick="buttonPressed(1)">Link 1</a><a onclick="buttonPressed(2)">Link 2</a><a onclick="buttonPressed(3)">Link 3</a><a onclick="buttonPressed(4)">Link 4</a></div>
 
Share this answer
 
v2
Comments
Member 7928594 18-Sep-11 14:53pm    
Well that's exeactly what I was looking for,
thank you soo much !!
Member 7928594 18-Sep-11 15:49pm    
But hey, what will happen if I want to use link tag [a] instead of <button> ? I've tryed to switch to a in the function argument and the switch-case [and the divs, of course..] but it doesn't work.. Can you give me any help please? Tali.
DaveAuld 19-Sep-11 1:20am    
<a onclick="buttonPressed(1)">Link 1</a> that is all you need to change.....
Have you visited this site; it has pretty much all the basics you need: http://www.w3schools.com
rkthiyagarajan 18-Sep-11 21:45pm    
5+...Nice one.
RaisKazi 19-Sep-11 5:37am    
Good Answer. my 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900