www.webdeveloper.com
Results 1 to 2 of 2

Thread: Modify JavaScript form element

  1. #1
    Join Date
    Jul 2013
    Posts
    2

    Modify JavaScript form element

    Hi All,

    if one changes a button type from type="button" to type="submit", it is my understanding that there would also be changes in the form action.

    How would I modify the current line:

    <form id="myForm" action="myAction.php" method="get">

    to something like this using javaScript:

    <form id="myForm" action="myAction.php" onsubmit="myFunction">


    Do I need to do something like this:

    document.getElementById("myForm").method="";
    document.getElementById("myForm").onsubmit="myFunction";

  2. #2
    Join Date
    Mar 2011
    Posts
    1,160
    An <input type="button"> has no defined function. You need to use JavaScript to assign a function to the button and, of course, create the corresponding function for it. <input type="submit"> will cause the parent <form> element's submit function to be executed, which can be intercepted and even cancelled by using the 'onsubmit' attribute to point to a function to be executed before the submission.

    At some point, your myFunction() is going to need the 'form' object, so your call to getElementById is one way of doing it. Typically, the object is passed to the function through the code in the 'onsubmit' attribute, as in:
    Code:
    <form id="myForm" action="myAction.php" method="get" onsubmit="return myFunction(this);">
    and then the JavaScript:
    Code:
     function myFunction(myForm) {
       if (myForm.something.value == '12') {
        return true;  // submit the form
       }
     return false;  // don't submit the form
     } // end myFunction()

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles