This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes How to show selected value, and name of list menu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to show selected value, and name of list menu" Watch "How to show selected value, and name of list menu" New topic
Author

How to show selected value, and name of list menu

Fritz Largosa
Ranch Hand

Joined: Sep 12, 2005
Posts: 70
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 ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61450
    
  67

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Fritz Largosa
Ranch Hand

Joined: Sep 12, 2005
Posts: 70
Works, thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to show selected value, and name of list menu