• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to show selected value, and name of list menu

 
Fritz Largosa
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • 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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Works, thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic