1

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();">
Chris
  • 1,863
  • 5
  • 30
  • 43
  • Does this answer your question? [JavaScript: Inline Script with SRC Attribute?](https://stackoverflow.com/questions/1056325/javascript-inline-script-with-src-attribute) – Ivar May 23 '23 at 11:04

3 Answers3

5

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>
rgthree
  • 7,217
  • 17
  • 21
  • Of course, I'm such an idiot, I knew that but for some reason didn't spot it until you pointed it out. I can see a couple of other people have posted the same answer - sorry everyone, idiotic mistake! – Chris Jan 20 '12 at 15:51
2

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?

Community
  • 1
  • 1
bububaba
  • 2,840
  • 6
  • 25
  • 29
1

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>
Andreas Eriksson
  • 8,979
  • 8
  • 47
  • 65