Click to See Complete Forum and Search --> : display a variable in a function


tyeh26
07-24-2008, 03:41 PM
I feel like this is a very simple task to do. yet, i don't actually know how to do it.

Say you have a form:


<form id = "cell">
<select id = "0 size="2">
<option value="one">two</option>
<option value="two">one</option>
</select>
<input type="button" onClick="show();">
</form>


What i want show to do, is to display the value of the select at a certain place on the page.

Say in side a div in a certain place.

so something like:

<p> The selected value is:
<script type="text/javascript">
//display the value
</script>
</p>

But of course this won't work because it will execute on load, and it won't wait for the value to be selected. So... how do i do this. Thanks so much.

Ayşe
07-24-2008, 03:50 PM
If I understand what you wanted:

<script type="text/javascript">
function f(){
var sel=document.getElementById('num');
var el=document.getElementById('tyeh26');
el.innerHTML=sel.options[sel.selectedIndex].value;
}
</script>
</head>
<body>
<form id = "cell">
<select id = "num" size="2" onchange="f()">
<option value="one">two</option>
<option value="two">one</option>
</select>
</form>

The selected value is: <p id="tyeh26"></p>

using the button:

<script type="text/javascript">
function f(){
var sel=document.getElementById('num');
var el=document.getElementById('tyeh26');
el.innerHTML=sel.options[sel.selectedIndex].value;
}
</script>
</head>
<body>
<form id = "cell">
<select id = "num" size="2">
<option value="one">two</option>
<option value="two">one</option>
</select>
<input type="button" value="click me" onClick="f();">

</form>

The selected value is: <p id="tyeh26"></p>

tyeh26
07-24-2008, 04:27 PM
el.innerHTML eh...
Cool thanks a bunch. I should review my javascript a bit.