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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CF Select and Javascript

Status
Not open for further replies.

clifftech

ISP
Joined
Nov 9, 2001
Messages
111
Location
US
This probably has an easy solution but I sure can't figure it out.

I have a CFSELECT tag that uses values from a CFQUERY tag. In the CFSELECT tag I'm using the "onchange" attribute to pass the selected value to a javascript funtion. I'm using the following code to in the javascript function so that the alert Window shows the selected value but all I get is "0" or "NaN". What am I doing wrong?

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

function formHandler(form, menu2){
var PROJ = menufrm.menu2.options[menufrm.menu2.selectedIndex].value;
alert(+PROJ);
}

</SCRIPT>



<cfquery name=&quot;queryname&quot; datasource=&quot;source&quot;>select ...</cfquery>

<cfform name=&quot;menufrm&quot; >


<cfselect query=&quot;queryname&quot; value=&quot;queryValue&quot; name=&quot;menu2&quot;
onchange=&quot;formHandler(this.form, this.form.menu2)&quot;>
<option> Unassigned</option>
</cfselect>

</cfform>


 
Try this in the function instead:
Code:
menufrm.menu2.options[menufrm.menu2.selectedIndex].text
Not sure if that works in all browsers though. Kevin
slanek@ssd.fsi.com
 
That didn't work either. It still shows &quot;NaN&quot; in the alert window.

What's funny is the javascript code below does result in the correct index number for the selected value.

var PROJ = menufrm.menu2.selectedIndex

Any other suggestions?
 
A lot of times getting JS to work correctly with CFForm elements can be a real headache. Perhaps you could try using a plain ol' SELECT and see if that works. Kevin
slanek@ssd.fsi.com
 
Still no luck with plain ol SELECT and FORM. What I found after experimenting is if the OPTION value is a numeric value it works fine. If the OPTION value is a text value it results in &quot;NaN&quot;.

 
Perhaps it's a browser thing because I'm using
Code:
options[index].text
in one of my apps and it works fine. I'm not doing anything different than what you've shown. I was using IE5.5 when I built it. (I've since installed IE6 and it works there too.) Kevin
slanek@ssd.fsi.com
 
I think your right. Could you copy this code and see if all the select options return the correct value. When I select A1234 I get &quot;NaN&quot;




<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function combineMenus(form, menu2) {
str = menu2.options[menu2.selectedIndex].text;
alert(+str)
return false;
}
</script>

<form name=&quot;menufrm&quot;>


<select name=&quot;menu2&quot; onChange=&quot;combineMenus(this.form, this.form.menu2)&quot; >

<option value=&quot;&quot;>Year</option>
<option value=&quot;1998&quot;>1998</option>
<option value=&quot;1999&quot;>1999</option>
<option value=&quot;A1234&quot;>A1234</option>
<option value=&quot;2001&quot;>2001</option>

</select>

</form>
 
Try this:
Code:
alert(str);

I think the &quot;+&quot; is the problem. I was getting the same results you were gettig, so I took the &quot;+&quot; out of the alert. Kevin
slanek@ssd.fsi.com
 
That did it. Thanks a million. I spent untold hours on that problem.
 
No sweat. Glad to help! Kevin
slanek@ssd.fsi.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top