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>

LVL 15
praneethaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Smart_ManCommented:
print it like on a printer or a form ?

please more details

waiting for your reply
0
Michel PlungjanIT ExpertCommented:
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

0
Smart_ManCommented:
can you specify more details. is my help welcomed ?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Michel PlungjanIT ExpertCommented:
I guessed Print as in show... Not as in print on printer
0
praneethaAuthor Commented:
 CountryName = formObj.CountryList.options[document.forms['frm'].CountryList.selectedIndex].text;
              document.getElementById("countryName").innerHTML=htmlEncode(CountryName);
                
0
Smart_ManCommented:
no objection
0
Michel PlungjanIT ExpertCommented:
So the code I suggested was correct.
0
Michel PlungjanIT ExpertCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Smart_ManCommented:
waiting for reply from the author
0
Michel PlungjanIT ExpertCommented:
We got the reply: delete question
0
Michel PlungjanIT ExpertCommented:
Yes objections.

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

i agree on that
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Applications

From novice to tech pro — start learning today.