We help IT Professionals succeed at work.

print Selected value from Select in HTML

if i have this in my ascx

   <select id="testList" name="testList" >
                          <options>
;;
;;;

                        </select>

and in same ascx i would like to print the selected value how do i do it.

 <td  colspan="1">
                   <%  print the select value here? %>
                    </td>

Comment
Watch Question

print it like on a printer or a form ?

please more details

waiting for your reply
Michel PlungjanIT Expert
SILVER EXPERT
Top Expert 2009

Commented:
Like this:
<select id="testList" name="testList"
onChange="document.getElementById('cellID').innerHTML=this.options[this.selectedIndex].value">
<options>
.
.
.
</select>
<td  colspan="1" id="cellID"></td>

Open in new window

can you specify more details. is my help welcomed ?
Michel PlungjanIT Expert
SILVER EXPERT
Top Expert 2009

Commented:
I guessed Print as in show... Not as in print on printer

Author

Commented:
 CountryName = formObj.CountryList.options[document.forms['frm'].CountryList.selectedIndex].text;
              document.getElementById("countryName").innerHTML=htmlEncode(CountryName);
                
no objection
Michel PlungjanIT Expert
SILVER EXPERT
Top Expert 2009

Commented:
So the code I suggested was correct.
IT Expert
SILVER EXPERT
Top Expert 2009
Commented:
This is not very clean

CountryName = formObj.CountryList.options[document.forms['frm'].CountryList.selectedIndex].text;
document.getElementById("countryName").innerHTML=htmlEncode(CountryName);

It should be

var formObj = document.forms['frm'];
var CountryName = formObj.CountryList.options[formObj.CountryList.selectedIndex].text;
document.getElementById("countryName").innerHTML=CountryName;

or cleaner:

var sel = document.forms['frm'].CountryList;
var CountryName = sel.options[sel.selectedIndex].text;
document.getElementById("countryName").innerHTML=CountryName;


why html encode? And where is that function specified?

and where do you want to call it?

My code will copy the text from the select whenever the user changes it.
If you want to copy the text onload of the page, you can use this:


<html>
<head>
<script type="text/javascript">
window.onload=function() {changeIt(document.frm.CountryList)}
function changeIt(sel) {
  document.getElementById('countryName').innerHTML=sel.options[sel.selectedIndex].text;
}
</script>
</head>
<body>
<form name="frm">
<select name="CountryList"
onChange="changeIt(this)">
<options>
.
.
.
</select>
</form>
<td  colspan="1" id="countryName"></td>

Open in new window

waiting for reply from the author
Michel PlungjanIT Expert
SILVER EXPERT
Top Expert 2009

Commented:
We got the reply: delete question
Michel PlungjanIT Expert
SILVER EXPERT
Top Expert 2009

Commented:
Yes objections.

My code answered the question and the last comment showed how to use the code in the actual setting.
after reviewing ... mplungjan .. deserves points...

i agree on that

Explore More ContentExplore courses, solutions, and other research materials related to this topic.