Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 3.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-14-2010, 01:02 PM   PM User | #1
pdilipm
New to the CF scene

 
Join Date: Apr 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
pdilipm is an unknown quantity at this point
Display Listbox

Hi,

I have an application wherein for one page I want to display a Listbox on selection of a radio button.
There should be 2 radio buttons and when the user clicks on the 2nd one "My Process", a list box should be displayed with several options.

Can anybody pls help me with this as I'm a beginner to JS..


Many thanks
Pooja.
pdilipm is offline   Reply With Quote
Old 04-14-2010, 01:47 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Here you are:-

Code:
<script type = "text/javascript">
function showListBox() {
document.getElementById("div1").style.display="block";
}
</script>

<form name = "myform">
Something <input type = "radio" name = "rad1">
My Process <input type = "radio" name = "rad1" onclick = "showListBox()">
<br><br>
<div id = "div1" style="display:none">
<select name = 'list1' id = 'list1' >
<option selected value=""> Choose A Fruit</option>
<option value='Mango'> Mango </option>
<option value='Apple'> Apple </option>
<option value='Orange'> Orange </option>
<option value='Watermelon'> Watermelon </option>
<option value='Plum'> Plum </option>
</select>
</div>
</form>
What happens if the user changes his mind and clicks the first button after selecting the second button?

"A man would do nothing, if he waited until he could do it so well that no one at all would find fault with what he has done." - Cardinal Newman
Philip M is offline   Reply With Quote
Old 04-14-2010, 06:08 PM   PM User | #3
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,762
Thanks: 29
Thanked 452 Times in 446 Posts
jmrker will become famous soon enough
Thumbs up Suggestion ...

Quote:
Originally Posted by Philip M View Post
Here you are:-
....
What happens if the user changes his mind and clicks the first button after selecting the second button?
....
My suggestion would be to hide the list again and test display status (block || none) to determine if listbox selection is to be ignored or not.

Code:
<script type = "text/javascript">
function showListBox(flag) {
  var sel = document.getElementById("div1");
  if (flag) { sel.style.display="block"; } else { sel.style.display='none'; }
}
</script>

<form name = "myform">
Something <input type = "radio" name = "rad1" onclick = showListBox(false)>
My Process <input type = "radio" name = "rad1" onclick = "showListBox(true)">
<br><br>
<div id = "div1" style="display:none">
<select name = 'list1' id = 'list1' >
<option selected value=""> Choose A Fruit</option>
<option value='Mango'> Mango </option>
<option value='Apple'> Apple </option>
<option value='Orange'> Orange </option>
<option value='Watermelon'> Watermelon </option>
<option value='Plum'> Plum </option>
</select>
</div>
</form>
jmrker is offline   Reply With Quote
Old 04-14-2010, 07:42 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by jmrker View Post
My suggestion would be to hide the list again and test display status (block || none) to determine if listbox selection is to be ignored or not.
Yes, but you need to set the selected index back to 0, otherwise the selection remains active (and would be submitted in a form) even if the div is hidden. So I had (but waiting for the OP to say what he wanted! )

Code:
<script type = "text/javascript">
function showListBox() {
document.getElementById("div1").style.display="block";
}

function hideListBox() {
document.getElementById("div1").style.display="none";
document.getElementById("list1").selectedIndex = 0;  // cancel any selection made
}

</script>

<form name = "myform">
Something <input type = "radio" name = "rad1" onclick = "hideListBox()">
My Process <input type = "radio" name = "rad1" onclick = "showListBox()">
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
pdilipm (04-15-2010)
Old 04-15-2010, 09:14 AM   PM User | #5
pdilipm
New to the CF scene

 
Join Date: Apr 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
pdilipm is an unknown quantity at this point
Philip M,
I tried yer code and it works beautifully. Only change I made is changing dropdown to multi-select list box. Yes, if the user changes his selection, I want the list box to hide.

Thanks alot for yer help
pdilipm is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:34 PM.


Advertisement
Log in to turn off these ads.