I have the following form(echoed through php), which when a radio button is selected, I want that value to be passed to a javascript function, which I can then deal with.

<form id=\"form1\" name=\"form1\" method=\"\" action=\"JavaScript:alterRecord()\">
<input name=\"radiobutton\" type=\"radio\" value=\"test1\" />Test 2<p>
<input name=\"radiobutton\" type=\"radio\" value=\"test2\" />Test 2<p>
<input name=\"radiobutton\" type=\"radio\" value=\"test3\" />Test 3<p>
<input name=\"radiobutton\" type=\"radio\" value=\"test4\" />Test 4
<input type=\"submit\" name=\"Submit\" value=\"Submit\" />

And the JavaScript

function alterRecord(value) {

I can not find out how to get the javascript to obtain the form submitted value.

Accepted Answer

If you want to do something in Javascript on submit, but not have the normal POST or GET request occur (which causes the old page to unload and a new page to load), make sure to do something like this:

    <style type="text/css">
      body { background-color: black; color: white; }
    <script type="text/javascript">
      function handleClick(event)
        if (console) console.info("handling click");  
        // for Firebug debugging, if you want it

    /* do something here */
        // I don't like alerts but it works everywhere

        if (console) console.info("handled click");  

        event.preventDefault(); // disable normal form submit behavior
        return false; // prevent further bubbling of event
      function doit()
        if (console) console.info("starting doit()");
        document.forms.myform.addEventListener("submit", handleClick, true);
        return true;
  <body onload="doit()">
    <form name="myform">
          <textarea name="textBox1" rows="10" cols="80">
'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe;
All mimsy were the borogoves,
And the mome raths outgrabe.
      <input type="submit" value="Update"/>

The lines event.preventDefault() and return false are key. I also like using addEventListener rather than hard-coding an onSubmit in the form. Just a matter of style/preference I guess. (although addEventListener does allow you to have multiple handlers.)

Written by Jason S
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki