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!

OnChange() Object Expected Error

Status
Not open for further replies.

LilProgrammerGirl

Programmer
Jun 24, 2004
81
US
Hi All,
I have a drop down list that I would like to have an OnChange Event where once it changes, I want the value to be stored in a session variable.

Here's my Drop Down:

Code:
<select name="PassFailSubSection" onchange="selectChanged(this.value);">
   <option value=" "></option>
   <option value="P">Pass</option>
   <option value="C">Coach</option>
   <option value="F">Fail</option>
 </select>

Then I am just trying to test the passing of the value to my function to see if I can at least do this and I get the error.
Here's my function:

Code:
function selectChanged(value) {
 alert(value);
 return false;
}

Any ideas why I am getting an Object Expected Error?

Thanks,
HaileyMarie
 
value is a reserved word in javascript, try this:
Code:
<script language=javascript>
function selectChanged(dropDownValue) {
 alert(dropDownValue);
 return false;
}
</script>
<select name="PassFailSubSection" onchange="selectChanged(this[this.selectedIndex].value);">
   <option value=" "></option>
   <option value="P">Pass</option>
   <option value="C">Coach</option>
   <option value="F">Fail</option>
 </select>

-kaht

banghead.gif
 
Kaht,
Thank you so much for the suggestion, but I am still getting "Object Expected". [neutral]

Hailey
 
can u give us the full HTML code, seems like it is something else that is generating this error...

Known is handfull, Unknown is worldfull
 
Yup... "Object expected" error means that function is not available. In 90% of cases this is caused by typos (cASe included) or f**ed HTML (unclosed tags, missing quote etc).
 
Try:

onchange="selectChanged(this.options[this.selectedIndex].value);
 
Okay Boelem! That worked! :)

So, now this is my option value:
Code:
<select name="PassFailSubSection" onchange="selectChanged(this.options[this.selectedIndex].value);">
  <option value=" "></option>
  <option value="Pass">P</option>
  <option value="Coach">C</option>
  <option value="Fail">F</option>
</select>

And this is my function:
Code:
function selectChanged(theValue) {
alert(theValue);
}

When I choose the value in the drop down the alert now pops up and says either "Pass", "Coach", or "Fail".

Now, what I have to do is assign theValue to a session variable - so I can pass it to my next page. I try this:

Code:
function selectChanged(theValue) {
<% session("subSectVal")=theValue %>;
alert( <%=Session.Contents("subSectVal")%>);
}

and it sends a pop-up alert on the change without an error - but the alert is empty.

What am I doing wrong here?

Thank you!

Hailey
 

You cannot assign values to server-side variables like this from the client-side. It is just not possible to do.

Hope this helps,
Dan
 
Hailey,

What you might try is to have a hidden IFRAME on your page, such as:

Code:
<iframe id='sessionSetter' height='0%'></iframe>

Then, from your javascript function, set the location of the iframe to a JSP (or ASP or whatever you're using) URL with the parameter "?myVal="+theValue tagged on to the end. Have THAT JSP set the session value.

Note that if you do it this way, that session value may still not be available in time for your alert message, but there are other ways to confirm that the value was set (also involving JavaScript, but not as direct as what you're trying).

'hope that helps.

--Dave
 
Is there any type of workaround? Is it possible to assign the value of a drop down selection to a session variable in the onChange event - or does the onChange even HAVE to point to a Javascript function?

Code:
<select name="PassFailSubSection" onchange="[COLOR=red]assign it here maybe?[/color]">
 
I'm going to assume you're writing a JSP, but even if it's something else, the same applies:

The application runs, building the HTML and JavaScript as it goes. Only when the application has reached bottom is the HTML considered complete. It is then posted client side. At this point, the server side in unreachable except by submitting or calling a new window with the URL set to another JSP (or whatever) or by setting the location of a same-page IFRAME to another JSP (or whatever).

So you can dynamically create the HTML and JavaScript, but you are trying to dynamically set server-side information. The only way, again, is through form submittal or window opening or iframe-location setting.

Your onchange event doesn't have to call a javascript FUNCTION per se, but it can only include javascript information (a few commands, for example).

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top