radhakrishan
asked on
Javascript in ASP (Enabling/disabling radio buttons based on drop down selection)
Hi,
Pls have a look at code below:
<select name="cboFLBP" onchange="DisplayFLBPContr ols()">
<option value="NA" <% if msFLBP="NA" then Response.Write "selected" %>>n/a</option>
<option value="FL" <% if msFLBP="FL" then Response.Write "selected" %>>Front
Load</option>
<option value="BP" <% if msFLBP="BP" then Response.Write "selected" %>>By
Pass</option>
</select>
<td class="Label" nowrap width="0">
<div id='divFLCreditNote' align="right"><font face="Arial" size="2">Credit Note</font></div>
</td>
<td align="left" nowrap width="90%">
Yes<input type=radio id='txtFLCreditNote' name='txtFLCreditNote' value='1' <%if miFLCreditNote = True then Response.Write " Checked "%>></INPUT>
No<input type=radio id='txtFLCreditNote' name='txtFLCreditNote' value='0' <%if miFLCreditNote = False then Response.Write " Checked "%>></INPUT>
</td>
function DisplayFLBPControls()
{ switch(formEdit.cboFLBP.va lue)
{
case 'NA':
divFLCreditNote.style.visi bility = 'hidden'
formEdit.txtFLCreditNote.s tyle.visib ility = 'hidden'
break;
case 'FL':
divFLCreditNote.style.visi bility = 'visible'
formEdit.txtFLCreditNote.s tyle.visib ility = 'visible'
break;
case 'BP':
divFLCreditNote.style.visi bility = 'hidden'
formEdit.txtFLCreditNote.s tyle.visib ility = 'hidden'
break;
}
}
Problem:
When u go to make changes: (Select "FL" from the drop down then "Credit Note" label is hidden but it displays "yes or No" radio buttons.
Why its display radio buttons when it should not ?
Can anybody pls help.
Pls have a look at code below:
<select name="cboFLBP" onchange="DisplayFLBPContr
<option value="NA" <% if msFLBP="NA" then Response.Write "selected" %>>n/a</option>
<option value="FL" <% if msFLBP="FL" then Response.Write "selected" %>>Front
Load</option>
<option value="BP" <% if msFLBP="BP" then Response.Write "selected" %>>By
Pass</option>
</select>
<td class="Label" nowrap width="0">
<div id='divFLCreditNote' align="right"><font face="Arial" size="2">Credit Note</font></div>
</td>
<td align="left" nowrap width="90%">
Yes<input type=radio id='txtFLCreditNote' name='txtFLCreditNote' value='1' <%if miFLCreditNote = True then Response.Write " Checked "%>></INPUT>
No<input type=radio id='txtFLCreditNote' name='txtFLCreditNote' value='0' <%if miFLCreditNote = False then Response.Write " Checked "%>></INPUT>
</td>
function DisplayFLBPControls()
{ switch(formEdit.cboFLBP.va
{
case 'NA':
divFLCreditNote.style.visi
formEdit.txtFLCreditNote.s
break;
case 'FL':
divFLCreditNote.style.visi
formEdit.txtFLCreditNote.s
break;
case 'BP':
divFLCreditNote.style.visi
formEdit.txtFLCreditNote.s
break;
}
}
Problem:
When u go to make changes: (Select "FL" from the drop down then "Credit Note" label is hidden but it displays "yes or No" radio buttons.
Why its display radio buttons when it should not ?
Can anybody pls help.
You code says if it's 'FL', they both should be visible. Are you getting any javascript errors at the bottom left corner of i.e. when you change the dropdown? It will turn into a yellow exclamation point.
seems like the problem is that you have 2 radiobuttons with the same name and javascript dosn't know wich one of them hide/show
i suggest using div to hide/unhide the radio buttons, this also hide tha labels 'yes/no', try something like this
'############### asp
<td align="left" nowrap width="90%">
<div id="divRadioButtons">
Yes<input type=radio id='txtFLCreditNote' name='txtFLCreditNote' value='1' <%if miFLCreditNote = True then Response.Write " Checked "%>></INPUT>
No<input type=radio id='txtFLCreditNote' name='txtFLCreditNote' value='0' <%if miFLCreditNote = False then Response.Write " Checked "%>></INPUT>
</div>
</td>
'############### javascript
function DisplayFLBPControls(){
switch(formEdit.cboFLBP.va lue){
case 'NA':
divFLCreditNote.style.visi bility = 'hidden';
divRadioButtons.style.visi bility = 'hidden';
break;
case 'FL':
divFLCreditNote.style.visi bility = 'visible';
divRadioButtons.style.visi bility = 'visible';
break;
case 'BP':
divFLCreditNote.style.visi bility = 'hidden';
divRadioButtons.style.visi bility = 'hidden';
break;
}
}
i suggest using div to hide/unhide the radio buttons, this also hide tha labels 'yes/no', try something like this
'############### asp
<td align="left" nowrap width="90%">
<div id="divRadioButtons">
Yes<input type=radio id='txtFLCreditNote' name='txtFLCreditNote' value='1' <%if miFLCreditNote = True then Response.Write " Checked "%>></INPUT>
No<input type=radio id='txtFLCreditNote' name='txtFLCreditNote' value='0' <%if miFLCreditNote = False then Response.Write " Checked "%>></INPUT>
</div>
</td>
'############### javascript
function DisplayFLBPControls(){
switch(formEdit.cboFLBP.va
case 'NA':
divFLCreditNote.style.visi
divRadioButtons.style.visi
break;
case 'FL':
divFLCreditNote.style.visi
divRadioButtons.style.visi
break;
case 'BP':
divFLCreditNote.style.visi
divRadioButtons.style.visi
break;
}
}
Hi radhakrishan,
When there are more than 1 radio button, that object becomes an array of object but and you have to refer to it like radiobutton[0]. When there is only 1 radio button, it is a single object and refer to it as radiobutton. So you js function should be
function DisplayFLBPControls()
{ switch(formEdit.cboFLBP.va lue)
{
case 'NA':
divFLCreditNote.style.visi bility = 'hidden'
for (var i=0; i<formEdit.txtFLCreditNote .length; i++) formEdit.txtFLCreditNote[i ].style.vi sibility = 'hidden';
break;
case 'FL':
divFLCreditNote.style.visi bility = 'visible'
for (var i=0; i<formEdit.txtFLCreditNote .length; i++) formEdit.txtFLCreditNote[i ].style.vi sibility = 'visible';
break;
case 'BP':
divFLCreditNote.style.visi bility = 'hidden'
for (var i=0; i<formEdit.txtFLCreditNote .length; i++) formEdit.txtFLCreditNote[i ].style.vi sibility = 'hidden';
break;
}
}
Cheers
Nickson
When there are more than 1 radio button, that object becomes an array of object but and you have to refer to it like radiobutton[0]. When there is only 1 radio button, it is a single object and refer to it as radiobutton. So you js function should be
function DisplayFLBPControls()
{ switch(formEdit.cboFLBP.va
{
case 'NA':
divFLCreditNote.style.visi
for (var i=0; i<formEdit.txtFLCreditNote
break;
case 'FL':
divFLCreditNote.style.visi
for (var i=0; i<formEdit.txtFLCreditNote
break;
case 'BP':
divFLCreditNote.style.visi
for (var i=0; i<formEdit.txtFLCreditNote
break;
}
}
Cheers
Nickson
ASKER
Msg for Nickson,
When I use, what u mention it works. But its shows me "Yes" or "No", no buttons next to it. Just labels.
Any advice:
This is the code:
<input type=radio id="txtFLCreditNote" name="txtFLCreditNote" Value='1' <%if miFLCreditNote = True then Response.Write " Checked "%>>Yes</INPUT>
<input type=radio id="txtFLCreditNote" name="txtFLCreditNote" value='0' <%if miFLCreditNote = False then Response.Write " Checked "%>>No</INPUT>
Again its just displaying "Yes" or "No" when they should be hidden.
Regards
Radio buttons need to have the same name so they form part of the same group (i.e. can only select one from the same group), but all elements should have unique ids.
You have the same name and the same id for the radio buttons which is why it's not working, try something like:
<td align="left" nowrap width="90%">
Yes<input type=radio id='txtFLCreditNote1' name='txtFLCreditNote' value='1' <%if miFLCreditNote = True then Response.Write " Checked "%>></INPUT>
No<input type=radio id='txtFLCreditNote2' name='txtFLCreditNote' value='0' <%if miFLCreditNote = False then Response.Write " Checked "%>></INPUT>
</td>
function DisplayFLBPControls()
{ switch(formEdit.cboFLBP.va lue)
{
case 'NA':
divFLCreditNote.style.visi bility = 'hidden'
formEdit.txtFLCreditNote1. style.visi bility = 'hidden'
formEdit.txtFLCreditNote2. style.visi bility = 'hidden'
break;
case 'FL':
divFLCreditNote.style.visi bility = 'visible'
formEdit.txtFLCreditNote1. style.visi bility = 'visible'
formEdit.txtFLCreditNote2. style.visi bility = 'visible'
break;
case 'BP':
divFLCreditNote.style.visi bility = 'hidden'
formEdit.txtFLCreditNote1. style.visi bility = 'hidden'
formEdit.txtFLCreditNote2. style.visi bility = 'hidden'
break;
}
}
You have the same name and the same id for the radio buttons which is why it's not working, try something like:
<td align="left" nowrap width="90%">
Yes<input type=radio id='txtFLCreditNote1' name='txtFLCreditNote' value='1' <%if miFLCreditNote = True then Response.Write " Checked "%>></INPUT>
No<input type=radio id='txtFLCreditNote2' name='txtFLCreditNote' value='0' <%if miFLCreditNote = False then Response.Write " Checked "%>></INPUT>
</td>
function DisplayFLBPControls()
{ switch(formEdit.cboFLBP.va
{
case 'NA':
divFLCreditNote.style.visi
formEdit.txtFLCreditNote1.
formEdit.txtFLCreditNote2.
break;
case 'FL':
divFLCreditNote.style.visi
formEdit.txtFLCreditNote1.
formEdit.txtFLCreditNote2.
break;
case 'BP':
divFLCreditNote.style.visi
formEdit.txtFLCreditNote1.
formEdit.txtFLCreditNote2.
break;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.