Reputation: 1883
The code I'm using is supposed to change the 'action' attribute for a form based on radio button selection. It doesn't work at the moment and I'm trying to work out why. According to my Firefox error console, the function 'submitForm' is not defined - but I'm sure it is defined!!
Can anyone help?
Code as follows:
Inside the head (q1 refers to the name attribute of the radio buttons):
<script type="text/javascript" src="scripts/javascript.js">
function submitForm() {
if (document.forms[0].q1[1].checked == true) {
document.forms[0].action = "q2.html";
}
else if (document.forms[0].q1[0].checked == true) {
document.forms[0].action = "q3.html";
}
else {
alert ('Please choose an option');
}
}
</script>
And the event handler:
<form method="post" id="question1" onsubmit="return submitForm();">
Upvotes: 2
Views: 2871
Reputation: 2870
Perhaps this is what you want:
<script type="text/javascript" src="scripts/javascript.js"></script>
<script type="text/javascript">
function submitForm() {
if (document.forms[0].q1[1].checked == true) {
document.forms[0].action = "q2.html";
}
else if (document.forms[0].q1[0].checked == true) {
document.forms[0].action = "q3.html";
}
else {
alert ('Please choose an option');
}
}
</script>
In general, you cannot have a script tag with an src and contents within the tag. Most browsers will see the src and ignore what's within the tag itself, or throw an error. See What does a script-Tag with src AND content mean?
Upvotes: 3
Reputation: 9027
Are you actually writing the code inside that script tag, or is that ripped from firefox?
Because you can't write JavaScript inside a script tag with a src
attribute. This is how it should look:
<script type="text/javascript" src="scripts/javascript.js"></script>
<script type="text/javascript">
function submitForm() {
if (document.forms[0].q1[1].checked == true) {
document.forms[0].action = "q2.html";
}
else if (document.forms[0].q1[0].checked == true) {
document.forms[0].action = "q3.html";
}
else {
alert ('Please choose an option');
}
}
</script>
Upvotes: 2
Reputation: 7273
You can't have a cript tag with a src
and script contents. Your browser is loading the script file, and ignoring the contents inside, thus your function is undefined. this is what you want:
<script type="text/javascript" src="scripts/javascript.js"></script>
<script type="text/javascript">
function submitForm() {
if (document.forms[0].q1[1].checked == true) {
document.forms[0].action = "q2.html";
}
else if (document.forms[0].q1[0].checked == true) {
document.forms[0].action = "q3.html";
}
else {
alert ('Please choose an option');
}
}
</script>
Upvotes: 6