cfsatx
asked on
Click Radio buttons to hide or show divs
I have three radio buttons
Clicking on the 1st one should show first div contents, second one should show the 2nd div contents and third one should show the 3rd div contents.
What's the best way to do this?
Clicking on the 1st one should show first div contents, second one should show the 2nd div contents and third one should show the 3rd div contents.
What's the best way to do this?
ASKER
Should I use OnClick to trigger the javascript function
<TR>
<TD align="right" nowrap>Alert Type:</TD>
<TD>
<cfinput id="button_1" class="buttonClass" name="buttonGroup" type="radio" value="G">Global &nbs p;
<cfinput id="button_2" class="buttonClass" name="buttonGroup" type="radio" value="D">Default &nb sp;
<cfinput id="button_3" class="buttonClass" name="buttonGroup" type="radio" value="C">Custom
</TD>
</TR>
<div class="buttonDiv" id="buttonDiv_1">
<TR>
<TD align="right" nowrap>First:</TD>
<TD>Div 1 content</TD>
</TR>
</div>
<div class="buttonDiv" id="buttonDiv_2">
<TR>
<TD align="right" nowrap>Second:</TD>
<TD>Div 2 content</TD>
</TR>
</div>
<div class="buttonDiv" id="buttonDiv_3">
<TR>
<TD align="right" nowrap>Third:</TD>
<TD>Div 3 content</TD>
</TR>
</div>
<TR>
<TD align="right" nowrap>Alert Type:</TD>
<TD>
<cfinput id="button_1" class="buttonClass" name="buttonGroup" type="radio" value="G">Global &nbs
<cfinput id="button_2" class="buttonClass" name="buttonGroup" type="radio" value="D">Default &nb
<cfinput id="button_3" class="buttonClass" name="buttonGroup" type="radio" value="C">Custom
</TD>
</TR>
<div class="buttonDiv" id="buttonDiv_1">
<TR>
<TD align="right" nowrap>First:</TD>
<TD>Div 1 content</TD>
</TR>
</div>
<div class="buttonDiv" id="buttonDiv_2">
<TR>
<TD align="right" nowrap>Second:</TD>
<TD>Div 2 content</TD>
</TR>
</div>
<div class="buttonDiv" id="buttonDiv_3">
<TR>
<TD align="right" nowrap>Third:</TD>
<TD>Div 3 content</TD>
</TR>
</div>
No the jquery code already handles that. It assigns this custom function to run when the state of any of the radio buttons changes:
$('.buttonClass').change( function(){
$('.buttonDiv').hide(); // hide all divs
var buttonNum = this.id.split("_")[1]; // get selected button number
$('#buttonDiv_'+ buttonNum).show(); // show selected div
});
$('.buttonClass').change( function(){
$('.buttonDiv').hide(); // hide all divs
var buttonNum = this.id.split("_")[1]; // get selected button number
$('#buttonDiv_'+ buttonNum).show(); // show selected div
});
ASKER
It shows all 3 div contents and doesn't hide the other div 2 and 3 when i click the first radio. Same is the case with other radio buttons
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am using the same names that you have given me for Div, Class, ids etc.,
ASKER
Okay...I did put the enire div within a <td> cell and it does work. Thanks much
<TR>
<TD align="right" nowrap>First:</TD>
<TD><div class="buttonDiv" id="buttonDiv_1">Div 1 content</div></TD>
</TR>
<TR>
<TD align="right" nowrap>Second:</TD>
<TD><div class="buttonDiv" id="buttonDiv_2">Div 2 content</div></TD>
</TR>
<TR>
<TD align="right" nowrap>Third:</TD>
<TD><div class="buttonDiv" id="buttonDiv_3">Div 3 content</div></TD>
</TR>
<TR>
<TD align="right" nowrap>First:</TD>
<TD><div class="buttonDiv" id="buttonDiv_1">Div 1 content</div></TD>
</TR>
<TR>
<TD align="right" nowrap>Second:</TD>
<TD><div class="buttonDiv" id="buttonDiv_2">Div 2 content</div></TD>
</TR>
<TR>
<TD align="right" nowrap>Third:</TD>
<TD><div class="buttonDiv" id="buttonDiv_3">Div 3 content</div></TD>
</TR>
ASKER
Number of TRs are not the same for each radio selection. what should I do in that case?
> I am using the same names that you have given me for Div, Class, ids etc.,
Right, but I'm guessing the invalid div/tr nestings in your code are breaking things. Try my original example, without all the table stuff. It was tested in IE8/FF7.
Right, but I'm guessing the invalid div/tr nestings in your code are breaking things. Try my original example, without all the table stuff. It was tested in IE8/FF7.
Posts crossed ...
> Number of TRs are not the same for each radio selection. what should I do in that case?
The code's pretty dynamic, so I'm not sure what you mean.
> Number of TRs are not the same for each radio selection. what should I do in that case?
The code's pretty dynamic, so I'm not sure what you mean.
ASKER
It works when I do something like this. But I would like to eliminate the extra TRs. Should I put a condition around the TR. If button_1, then show the 1st TR...etc.,
<TR>
<TD align="right" nowrap></TD>
<TD>
<div class="buttonDiv" id="buttonDiv_1">
Facility Name: Enterprise Wide<br>
Install Name: Enterprise Wide
</div>
</TD>
</TR>
<TR>
<TD align="right" nowrap></TD>
<TD>
<div class="buttonDiv" id="buttonDiv_2">
Country: &nbs p; &n bsp;
<cfselect name="countrycode"
bind="cfc:dmis.getCountry( )" bindonload="true"
display="Facility_Country_ Code"
value="Facility_Country_Co de"
required="yes" selected="US" style="width:200" /><br>
State: &n bsp; &nbs p; &n bsp;
<cfselect name="statecode"
bind="cfc:dmis.getState({c ountrycode })" bindonload="false"
display="Facility_State_Co de"
value="Facility_State_Code " style="width:200" /> <br>
Facility Name:
<cfselect name="facilityname"
bind="cfc:dmis.getFacility Name({coun trycode},{ statecode} )" bindonload="false"
display="Facility_Name"
value="Facility_Name" style="width:300" /><br>
Install Name:
<cfselect name="installationname"
bind="cfc:dmis.getInstalla tionName({ countrycod e},{facili tyname})" bindonload="false"
display="Installation_Name "
value="Installation_Name" style="width:300" />
</div>
</TD>
</TR>
<TR>
<TD align="right" nowrap></TD>
<TD>
<div class="buttonDiv" id="buttonDiv_1">
Facility Name: Enterprise Wide<br>
Install Name: Enterprise Wide
</div>
</TD>
</TR>
<TR>
<TD align="right" nowrap></TD>
<TD>
<div class="buttonDiv" id="buttonDiv_2">
Country:
<cfselect name="countrycode"
bind="cfc:dmis.getCountry(
display="Facility_Country_
value="Facility_Country_Co
required="yes" selected="US" style="width:200" /><br>
State: &n
<cfselect name="statecode"
bind="cfc:dmis.getState({c
display="Facility_State_Co
value="Facility_State_Code
Facility Name:
<cfselect name="facilityname"
bind="cfc:dmis.getFacility
display="Facility_Name"
value="Facility_Name" style="width:300" /><br>
Install Name:
<cfselect name="installationname"
bind="cfc:dmis.getInstalla
display="Installation_Name
value="Installation_Name" style="width:300" />
</div>
</TD>
</TR>
ASKER
<TR>
<TD align="right" nowrap></TD>
<TD>
<div class="buttonDiv" id="buttonDiv_3">
Search Term:
<cfinput type="text" name="facname" autosuggest="cfc:dmis.getF acilitiesB yName({cfa utosuggest value})" autosuggestMinLength = "2"
maxresultsdisplayed="500" style="width:300" >
</div>
</TD>
</TR>
<TD align="right" nowrap></TD>
<TD>
<div class="buttonDiv" id="buttonDiv_3">
Search Term:
<cfinput type="text" name="facname" autosuggest="cfc:dmis.getF
maxresultsdisplayed="500" style="width:300" >
</div>
</TD>
</TR>
No, you can't use a cfif without resubmitting the page. But it seems like it'd make more sense to assign the class and "id" to the TR instead (IF you really need both).
ie
<TR class="buttonDiv" id="buttonDiv_1"> ....
I'd recommend changing the prefix from "div..." to "row" too. So it's more intuitive.
ie
<TR class="buttonDiv" id="buttonDiv_1"> ....
I'd recommend changing the prefix from "div..." to "row" too. So it's more intuitive.
ASKER
Perfect...Works like a champ now !! Thanks much !!!
Open in new window