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();">
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();">
Share
Improve this question
asked Jan 20, 2012 at 15:43
ChrisChris
1,8836 gold badges30 silver badges44 bronze badges
1
- Does this answer your question? JavaScript: Inline Script with SRC Attribute? – Ivar Commented May 23, 2023 at 11:04
3 Answers
Reset to default 6You 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>
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?
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>