Calling Javascript from a html form

Joshxtothe4 :

I am basing my question and example on Jason's answer in this question

I am trying to avoid using an eventListener, and just to call handleClick onsubmit, when the submit button is clicked.

Absolutely nothing happens with the code I have.

Why is handleClick not being called?

<html>
  <head>
    <script type="text/javascript">
      function getRadioButtonValue(rbutton)
      {
        for (var i = 0; i < rbutton.length; ++i)
        { 
          if (rbutton[i].checked)
            return rbutton[i].value;
        }
        return null;
      }

      function handleClick(event)
      {
        alert("Favorite weird creature: "+getRadioButtonValue(this["whichThing"]));
        event.preventDefault(); // disable normal form submit behavior
        return false; // prevent further bubbling of event
      }
    </script>
  </head>
<body>
    <form name="myform" onSubmit="JavaScript:handleClick()">
      <input name="Submit"  type="submit" value="Update" onClick="JavaScript:handleClick()"/>
      Which of the following do you like best?
      <p><input type="radio" name="whichThing" value="slithy toves" />Slithy toves</p>
      <p><input type="radio" name="whichThing" value="borogoves" />Borogoves</p>
      <p><input type="radio" name="whichThing" value="mome raths" />Mome raths</p>
    </form>
</body>
</html>

edit:

Please do not suggest a framework as a solution.

Here are the relevant changes I have made to the code, which results in the same behavior.

      function handleClick()
      {
        alert("Favorite weird creature: "+getRadioButtonValue(document.myform['whichThing'])));
        event.preventDefault(); // disable normal form submit behavior
        return false; // prevent further bubbling of event
      }
    </script>
  </head>
<body>
<form name="aye">;
      <input name="Submit"  type="submit" value="Update" action="JavaScript:handleClick()"/>
      Which of the following do you like best?
      <p><input type="radio" name="whichThing" value="slithy toves" />Slithy toves</p>
      <p><input type="radio" name="whichThing" value="borogoves" />Borogoves</p>
      <p><input type="radio" name="whichThing" value="mome raths" />Mome raths</p>
    </form>
Stephen Wrighton :

In this bit of code:

getRadioButtonValue(this["whichThing"]))

you're not actually getting a reference to anything. Therefore, your radiobutton in the getradiobuttonvalue function is undefined and throwing an error.

EDIT To get the value out of the radio buttons, grab the JQuery library, and then use this:

  $('input[name=whichThing]:checked').val() 

Edit 2 Due to the desire to reinvent the wheel, here's non-Jquery code:

var t = '';
for (i=0; i<document.myform.whichThing.length; i++) {
     if (document.myform.whichThing[i].checked==true) {
         t = t + document.myform.whichThing[i].value;
     }
}

or, basically, modify the original line of code to read thusly:

getRadioButtonValue(document.myform.whichThing))

Edit 3 Here's your homework:

      function handleClick() {
        alert("Favorite weird creature: " + getRadioButtonValue(document.aye.whichThing));
        //event.preventDefault(); // disable normal form submit behavior
        return false; // prevent further bubbling of event
      }
    </script>
  </head>
<body>
<form name="aye" onSubmit="return handleClick()">
     <input name="Submit"  type="submit" value="Update" />
     Which of the following do you like best?
     <p><input type="radio" name="whichThing" value="slithy toves" />Slithy toves</p>
     <p><input type="radio" name="whichThing" value="borogoves" />Borogoves</p>
     <p><input type="radio" name="whichThing" value="mome raths" />Mome raths</p>
</form>

Notice the following, I've moved the function call to the Form's "onSubmit" event. An alternative would be to change your SUBMIT button to a standard button, and put it in the OnClick event for the button. I also removed the unneeded "JavaScript" in front of the function name, and added an explicit RETURN on the value coming out of the function.

In the function itself, I modified the how the form was being accessed. The structure is: document.[THE FORM NAME].[THE CONTROL NAME] to get at things. Since you renamed your from aye, you had to change the document.myform. to document.aye. Additionally, the document.aye["whichThing"] is just wrong in this context, as it needed to be document.aye.whichThing.

The final bit, was I commented out the event.preventDefault();. that line was not needed for this sample.

EDIT 4 Just to be clear. document.aye["whichThing"] will provide you direct access to the selected value, but document.aye.whichThing gets you access to the collection of radio buttons which you then need to check. Since you're using the "getRadioButtonValue(object)" function to iterate through the collection, you need to use document.aye.whichThing.

See the difference in this method:

function handleClick() {
   alert("Direct Access: " + document.aye["whichThing"]);
   alert("Favorite weird creature: " + getRadioButtonValue(document.aye.whichThing));
   return false; // prevent further bubbling of event
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a PHP API from HTML Form?

From Dev

HTML button onClick listener calling HTML form onSubmit javascript function

From Dev

Issue with calling a javascript file from a HTML page

From Dev

Will adding onclick in HTML and calling from Javascript conflict?

From Dev

calling javascript function with an argument from html

From Dev

Undefined value when calling an input from a HTML form from PHP

From Dev

Ajax calling controller to send a html table of objects form from view

From Dev

Calling Form From VBA

From Dev

How to separate HTML from Javascript in this form?

From Dev

Passing data from an html form into a Javascript object

From Dev

JavaScript not taking values from HTML form

From Dev

Output HTML Form Element From Javascript

From Dev

Getting data from the HTML form into JavaScript

From Dev

Get values from html form to JavaScript objects

From Dev

how to get value from html form to javascript

From Dev

Passing javascript valiables from HTML form to PayPal

From Dev

getting dynamic values from html form to javascript

From Dev

Pass PHP variable from HTML form to Javascript

From Dev

HTML/Javascript addEventListener not calling?

From Dev

Javascript in HTML not calling correctly

From Dev

Calling a JavaScript function in HTML?

From Dev

javascript : built an html form from json, but can't update the form

From Dev

calling javascript function defined in html script tag from angular controller

From Dev

Calling a Javascript function from an external js file in the html file

From Dev

Calling JIRA REST Api from Javascript in local HTML file

From Dev

react-native-webview Calling a specific javascript function from the html

From Dev

Calling Javascript function from external file when loading html page

From Dev

Calling/Invoking a Javascript function from python a flask function within html

From Dev

Calling javascript functions from html code that I inject

Related Related

  1. 1

    Calling a PHP API from HTML Form?

  2. 2

    HTML button onClick listener calling HTML form onSubmit javascript function

  3. 3

    Issue with calling a javascript file from a HTML page

  4. 4

    Will adding onclick in HTML and calling from Javascript conflict?

  5. 5

    calling javascript function with an argument from html

  6. 6

    Undefined value when calling an input from a HTML form from PHP

  7. 7

    Ajax calling controller to send a html table of objects form from view

  8. 8

    Calling Form From VBA

  9. 9

    How to separate HTML from Javascript in this form?

  10. 10

    Passing data from an html form into a Javascript object

  11. 11

    JavaScript not taking values from HTML form

  12. 12

    Output HTML Form Element From Javascript

  13. 13

    Getting data from the HTML form into JavaScript

  14. 14

    Get values from html form to JavaScript objects

  15. 15

    how to get value from html form to javascript

  16. 16

    Passing javascript valiables from HTML form to PayPal

  17. 17

    getting dynamic values from html form to javascript

  18. 18

    Pass PHP variable from HTML form to Javascript

  19. 19

    HTML/Javascript addEventListener not calling?

  20. 20

    Javascript in HTML not calling correctly

  21. 21

    Calling a JavaScript function in HTML?

  22. 22

    javascript : built an html form from json, but can't update the form

  23. 23

    calling javascript function defined in html script tag from angular controller

  24. 24

    Calling a Javascript function from an external js file in the html file

  25. 25

    Calling JIRA REST Api from Javascript in local HTML file

  26. 26

    react-native-webview Calling a specific javascript function from the html

  27. 27

    Calling Javascript function from external file when loading html page

  28. 28

    Calling/Invoking a Javascript function from python a flask function within html

  29. 29

    Calling javascript functions from html code that I inject

HotTag

Archive