Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting value from drop down options 1

Status
Not open for further replies.

cumap

IS-IT--Management
Jul 9, 2007
268
US
hello all,

I'm having this problem trying to get a value from a select drop down options using js.
Code:
function Do_PlayVideoNum(camNum) {
    document.write(iCam.value);
	document.close();
}
and here is the drop down
Code:
         <select name="iCam" onchange="Do_PlayVideoNum(1);">
            <option value=""><- Select -></option>
            <% 
		for cam=1 to 10
            %>
            <option value="<% =cam %>">Camera <% =cam %></option>
            <% 	
		next
            %>
         </select>

I keep getting a result = undefine, why?

thanks!
 
Hi

There is that iCam variable set ? This would be syntactically correct :
Code:
iCam=document.getElementById('iCam')
document.write(iCam.options[iCam.selectedIndex].value);

<select name="iCam" id="iCam" onchange="Do_PlayVideoNum(1);">
But logically probably still wrong. You can not use [tt]document.write()[/tt] like that.

Feherke.
 
I just realized that I have more than one select option which I named them all the same "iCam". I change the name by doing this
Code:
function Do_PlayVideoNum(camNum) {
iCam=document.getElementById('iCam_'+camNum)    
document.write(iCam.value);
    document.close();
}

Code:
<select name="iCam_1" onchange="Do_PlayVideoNum(1);">
            <option value=""><- Select -></option>
            <%
        for cam=1 to 10
            %>
            <option value="<% =cam %>">Camera <% =cam %></option>
            <%     
        next
            %>
</select>
<select name="iCam_2" onchange="Do_PlayVideoNum(2);">
            <option value=""><- Select -></option>
            <%
        for cam=1 to 10
            %>
            <option value="<% =cam %>">Camera <% =cam %></option>
            <%     
        next
            %>
</select>

Now, I got an empty return...??????
 
oh, I forgot to add the id name in the select query too. Be assured that I did and tested.
 
I got it now, here's the output
Code:
function Do_PlayVideoNum(camNum) {
	
	var m = document.getElementById("iCam_" + camNum);
	document.write(m.selectedIndex)
}

the rest is the same. I guess the missing piece is the SELECTEDINDEX.

thanks anyway.
 
oh. also Feherke, I tried your document.write(iCam.options[iCam.selectedIndex].value); but somehow it doesn't return the output but blank. weird?
 
Hi

Well, that works for me as expected.

Where expected means, the document is replaced with the [tt]value[/tt] of the selected [tt]option[/tt].

You can use [tt]document.write()[/tt] only while the current document is rendered. Once finished, the [tt]document[/tt] is closed and further [tt]document.write()[/tt] calls will automatically open a new [tt]document[/tt]. Which will replace the current one.

As the [tt]onchange[/tt] event handler is executed after the document was rendered, the [tt]document.write()[/tt] will make the document "disappear", replacing it.

Are you sure that is what you expect too ?

Note, there is a markup error, but probably will not mess the things up :
Code:
[s]<option value=""><- Select -></option>[/s]
<option value="">[red]&lt;[/red]- Select -[red]&gt;[/red]</option>
For more debugging post your generated HTML ( as is displayed by your browser's View page source command ), or give us an URL to a publicly available copy of the page.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top