Forum Moderators: open

Message Too Old, No Replies

Displaying different message based on selected value

         

SilverLining

3:15 pm on May 3, 2010 (gmt 0)

10+ Year Member



What I'm trying to achieve is to display a different message, based on what option is selected within a form. The message is displayed on a redirect "Thank you" page.

<select name="rsvp" id="rsvp">
<option value="">Select</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>


On the redirected page I have the following function:

<script type="text/javascript">
function displayMsg() {
var attendingYes = "Yes";
var attendingNo = "No";
if (document.getElementById('rsvp').value == 'Yes') {
document.write(attendingYes);
}
else {
document.write(attendingNo);
}
window.onload = displayMsg;
}
</script>


Error: Uncaught TypeError: Cannot read property 'value' of null

I added an alert, which did not display, so the function does not run. The reason I added the onload inside the function is that I do not want to edit the templates which affect other pages.

See the mistake?

Fotiman

4:11 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Presumably there is another call to displayMsg somewhere else which triggers the first check. This call will end up replacing your entire document (because you are calling document.write after the page has loaded). Then your displayMsg function runs again, though 'rsvp' no longer exists because you've replaced the document when you called document.write? Just a guess... hard to tell without seeing more.

Long story short, don't use document.write. Ever.