lgduong
asked on
Jquery to hide previous div and open new div
I am able to open and hide 2 divs. See my code. However, if I have multiple div, how do I know which div was open so that if I click on another div, it would know to close the open div.
For example, I have a
Click here 1
Click here 2
Click here 3
Click here 4
and so forth
Let say a div is open (it could be any of div). If I click on "click here 2", it would know close the open div and open div 2.
My code for open and close between 2 divs:
<div id="first-div-section" style="display: none"></div>
<div id="second-div-section" style="display: none"></div>
<script type="text/javascript">
$(function()
{
$("#first-div-section").cl ick(functi on()
{
$("#second-div-section").h ide();
$("#first-div-section").an imate({ height:'600px' }, 'slow');
}
$("#second-div-section").c lick(funct ion()
{
$("#first-div-section").hi de();
$("#second-div-section").a nimate({ height:'600px' }, 'slow');
}
}
</script>
For example, I have a
Click here 1
Click here 2
Click here 3
Click here 4
and so forth
Let say a div is open (it could be any of div). If I click on "click here 2", it would know close the open div and open div 2.
My code for open and close between 2 divs:
<div id="first-div-section" style="display: none"></div>
<div id="second-div-section" style="display: none"></div>
<script type="text/javascript">
$(function()
{
$("#first-div-section").cl
{
$("#second-div-section").h
$("#first-div-section").an
}
$("#second-div-section").c
{
$("#first-div-section").hi
$("#second-div-section").a
}
}
</script>
There are two ways of doing this
1. Remember which div was open you can do this by assigning a class to it - that way any div that has that class is the open one and you can do what needs to be done and remove the class when it is closed
2. If you want all divs to be closed except the one that has been clicked on then close all divs first then open the one that has been clicked.
The latter is shown below
That should do it.
1. Remember which div was open you can do this by assigning a class to it - that way any div that has that class is the open one and you can do what needs to be done and remove the class when it is closed
2. If you want all divs to be closed except the one that has been clicked on then close all divs first then open the one that has been clicked.
The latter is shown below
<div id="first-div-section" class="divtotoggle" style="display: none"></div>
<div id="second-div-section" class="divtotoggle" style="display: none"></div>
The script<script type="text/javascript">
$(function()
{
$('div.divtotoggle').click(function () {
$('div.divtogoggle').hide();
$(this).animate({ height:'600px' }, 'slow');
});
}
</script>
That should do it.
ASKER
julianH,
Your second mention work. This is a good logic that I didn't think of, but should have.
However, that is not exactly how I want it. Your method doesn't take into consideration that the div (class: divtotoggle) are hidden/invisible so how can it be click. The initial display is set to "none".
Maybe I wasn't clear in my explanation. I need the "click here" to be the trigger to hide/show the other div. How do I pass the "first-div-section" as an argument/parameter into the "test" onclick?? Can that be done?
This is what I have done:
<div id="test" style="text-decoration:und erline; font-weight:bold; color:red;">Click here to show div 1</div>
<script type="application/javascri pt">
$('#test').click(function( )
{
$('#first-div-section').to ggle();
});
</script>
The old method using javascript:
<img src="...." onclick="ExpandCollapse('f irst-div-s ection'); " />
function ExpandCollapse(divID)
{
if(document.getElementById (divID).st yle.displa y == "none")
{
document.getElementById(di vID).style .display = "block";
}
else
{
document.getElementById(di vID).style .display = "none";
}
}
Your second mention work. This is a good logic that I didn't think of, but should have.
However, that is not exactly how I want it. Your method doesn't take into consideration that the div (class: divtotoggle) are hidden/invisible so how can it be click. The initial display is set to "none".
Maybe I wasn't clear in my explanation. I need the "click here" to be the trigger to hide/show the other div. How do I pass the "first-div-section" as an argument/parameter into the "test" onclick?? Can that be done?
This is what I have done:
<div id="test" style="text-decoration:und
<script type="application/javascri
$('#test').click(function(
{
$('#first-div-section').to
});
</script>
The old method using javascript:
<img src="...." onclick="ExpandCollapse('f
function ExpandCollapse(divID)
{
if(document.getElementById
{
document.getElementById(di
}
else
{
document.getElementById(di
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, that is similar to what I am looking for. Thanks.
you are welcome
Here's a jsfiddle example that uses an unordered list where the list items are clickable, but the idea is the same. The list items expand in height when clicked. Note how little jquery is needed to accomodate any number of clickable child elements.
http://jsfiddle.net/tommyBoy/4R3mh/