How to properly use javascript with SUBMIT?

Ken Ingram

At one point in the last 24 hours, I had a working function called get_contact_info which returned the contents of a series of text inputs. Suddenly the code stopped returning the values and began returning "undefined" as the element values, though noting had been changed in the code or html.

custom.js contains the following

var first_name, last_name, email, phone_number;
var contact_info = [];

function get_contact_info(){
    alert("This is the function called by the button");
}
/*  contact_info[0] = document.getElementsByName('fname').value;
    contact_info[1] = document.getElementsByName('lname').value;
    contact_info[2] = document.getElementsByName('email').value;
    contact_info[3] = document.getElementsByName('phone').value;

    alert("Name = " + contact_info[0] + " " + contact_info[1]);
    alert("Email = " + contact_info[2] + ", Phone Number " + contact_info[3]);
};*/


}());

My html is the following:

        <form>
        <table>
            <tbody>
                <tr><div class="custom text" style="color: red"><strong>Please enter your contact info to create a quote</strong></div></tr>
                <tr><td></td></tr>
                <tr>
                    <td><strong>First Name:</strong></td>
                    <td><input type="text" id="fname" value="Joe"/></td>
                </tr>
                <tr>
                    <td><strong>Last Name:</strong></td>
                    <td><input type="text" id="lname" value="Johnson"/></td>
                </tr>
                <tr>
                    <td><strong>Email:</strong></td>
                    <td><input type="text" id="email" value="[email protected]"/></td>                        
                </tr>
                <tr>
                    <td><strong>Phone Number:</strong></td>
                    <td><input type="text" id="phone" size="15" value="555-1212"/></td>     
                </tr>

                <tr>
                    <td><input id="contact_info" type="submit" value="Set My Contact Info" onClick="get_contact_info()" /></td>                     
                </tr>
            </tbody>                
        </table>
    </form>

Am I implementing this incorretly? I am attempting to teach myself basic javascript but I seem to be failing on this piece and don't see where.

Using Firebug in Firefox v30.0, a console call to document.getElementsByName('fname').value; returns "undefined" document.getElementsByName('fname'); returns a NodeList, but no elements.

sigh

RobG

The code you posted has syntax errors, and the part that seems to be throwing an error is commented out. As others have said, you have a problem with getElementsByName and getElementById. But your issues go further than that.

A form can be submitted without clicking the submit button, so it's usual to attach listeners that should run when the form is submitted to the form's submit handler. It also helps to pass a reference to the form using this (the reason will become clear later):

<form onsubmit="get_contact_info(this)" ...>

Form controls are only submitted if they have a name, giving them an ID is usually unnecessary, so change the IDs to names:

  <td><input type="text" name="fname" value="Joe"></td>

And the submit button becomes:

<input type="submit" value="Set My Contact Info">

Now you also have a very convenient way to access the form controls using their name as properties of the form. Since a reference to the form was passed in the call (see above), the function can be:

var contact_info = [];

function get_contact_info(form) {
  contact_info[0] = form.fname.value;
  contact_info[1] = form.lname.value;
  contact_info[2] = form.email.value;
  contact_info[3] = form.phone.value;

  alert("Name = " + contact_info[0] + " " + contact_info[1]);
  alert("Email = " + contact_info[2] + ", Phone Number " + contact_info[3]);
}

Also note that getElementsByName returns a live NodeList that doesn't have a value property, so if you were going to use it, you'd use it like:

document.getElementsByName('email')[0].value

You could also create the array using an array literal:

contact_info = [form.fname.value, form.lname.value, form.email.value, form.phone.value];

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to properly use variadic functions

분류에서Dev

How to properly use gofstat in R?

분류에서Dev

How to use multiple (re-)inheritance properly

분류에서Dev

How use orWhere in laravel elequent properly?

분류에서Dev

How do I properly use a Callback in this code? (Kivy)

분류에서Dev

How to properly use same text sections across multiple html pages?

분류에서Dev

How do I use WL.Client.getID() properly?

분류에서Dev

How to properly use -display when starting a program from a shell

분류에서Dev

How to properly use the Red/System dialect from the pure Red code?

분류에서Dev

How to use Meta attribute get_latest_by properly with pk in django?

분류에서Dev

How can I encode the parameters of a javascript function properly?

분류에서Dev

MySQL properly use of if exists

분류에서Dev

How to use javascript to preload images

분류에서Dev

How to use php inside of javascript?

분류에서Dev

How to use javascript with selenium python

분류에서Dev

Submit button setting a value in javascript

분류에서Dev

Javascript auto submit form not work

분류에서Dev

How can I disable someone from loading a page in an iframe and using javascript to submit?

분류에서Dev

Trying to submit one Javascript form, and two forms submit instead

분류에서Dev

How to properly use a change function on react button and not have any unused variables

분류에서Dev

How to: Properly use PHP to encode data into JSON format, and request the data with jquery/ajax

분류에서Dev

How to use Closure as an Anonymous Function like on Javascript?

분류에서Dev

How to use associative array in JavaScript to retrieve users?

분류에서Dev

How to use javascript function for multiple tasks?

분류에서Dev

How to use named Array element in javascript?

분류에서Dev

How to use parent and child Constructors in Javascript?

분류에서Dev

How to use JavaScript variable value as Object selector

분류에서Dev

How to use Object.deliverChangeRecords method in javascript

분류에서Dev

How to use Swipedown event using javascript?

Related 관련 기사

  1. 1

    How to properly use variadic functions

  2. 2

    How to properly use gofstat in R?

  3. 3

    How to use multiple (re-)inheritance properly

  4. 4

    How use orWhere in laravel elequent properly?

  5. 5

    How do I properly use a Callback in this code? (Kivy)

  6. 6

    How to properly use same text sections across multiple html pages?

  7. 7

    How do I use WL.Client.getID() properly?

  8. 8

    How to properly use -display when starting a program from a shell

  9. 9

    How to properly use the Red/System dialect from the pure Red code?

  10. 10

    How to use Meta attribute get_latest_by properly with pk in django?

  11. 11

    How can I encode the parameters of a javascript function properly?

  12. 12

    MySQL properly use of if exists

  13. 13

    How to use javascript to preload images

  14. 14

    How to use php inside of javascript?

  15. 15

    How to use javascript with selenium python

  16. 16

    Submit button setting a value in javascript

  17. 17

    Javascript auto submit form not work

  18. 18

    How can I disable someone from loading a page in an iframe and using javascript to submit?

  19. 19

    Trying to submit one Javascript form, and two forms submit instead

  20. 20

    How to properly use a change function on react button and not have any unused variables

  21. 21

    How to: Properly use PHP to encode data into JSON format, and request the data with jquery/ajax

  22. 22

    How to use Closure as an Anonymous Function like on Javascript?

  23. 23

    How to use associative array in JavaScript to retrieve users?

  24. 24

    How to use javascript function for multiple tasks?

  25. 25

    How to use named Array element in javascript?

  26. 26

    How to use parent and child Constructors in Javascript?

  27. 27

    How to use JavaScript variable value as Object selector

  28. 28

    How to use Object.deliverChangeRecords method in javascript

  29. 29

    How to use Swipedown event using javascript?

뜨겁다태그

보관