• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to show selected value, and name of list menu

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to be able to show the selected value and name of the selected value in the list/menu box, but I cannot figure it out. A sample code is below. I have clearly marked the code which I cannot figure out and need help in. Please give me some suggestions if you have some. Thanks.

----------------------------------------------------------------
<script type="text/javascript" >
function show_selection() {

var index= document.select.selectedIndex;

/********Problem Below**************/
var value = I want to get the selected value(1,2,3,4)
var name = I want to get the the selected name(First,Second,Third,Fourth)
/**********Problem Above****************/

document.write(index);
document.write(value);
document.write(name);

}
</script>


<select name="select" size="1" onclck="show_selection()">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
<option value="4">Fourth</option>
</select>

---------------------------------------------------------------

[ May 16, 2007: Message edited by: Fritz Largosa ]

[ May 16, 2007: Message edited by: Fritz Largosa ]
[ May 16, 2007: Message edited by: Fritz Largosa ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly, never use document.write after the page has loaded; it will replace your document, not augment it.

Secondly, onclick is the wrong event to use assuming you want the result of a selection; use onchange.

So here's a possible solution. Change the <select> element to:


Note that this uses onchange and also uses accepted naming conventions for the function name. The argument this passes a reference to the select element to the handler.

The function (using an alert) could then be written as:

[ May 16, 2007: Message edited by: Bear Bibeault ]
 
Fritz Largosa
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Works, thanks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic