2

I have an HTML form that I would like to make interact with some JavaScript:

...
<form name="signup">
    <label id="email" for="email" placeholder="Enter your email...">Email: </label>
    <input type="email" name="email" id="email" />
    <br />
    <input type="submit" name="submit" id="submit" value="Signup" onclick="signup()"/>
</form>
...

I have some JavaScript that I want to take the entered email address and store it in an array (it is currently inline with my HTML hence the script tags):

<script type="text/javascript" charset="utf-8">
        var emailArray = [];
        function signup(){
            var email = document.signup.email.value;
            emailArray.push(email);
            alert('You have now stored your email address');
            window.open('http://google.com');
                    console.log(emailArray[0]);
        }
</script>

I was hoping that this simple script would store the email in emailArray but the console remains empty throughout the execution.

What is wrong with this code?

4
  • 1
    Is the DOM present when you access it and did you try document.getElementById('email').value;? Commented Jan 21, 2014 at 9:37
  • @LarsBeck — The question clearly demonstrates that it is. Commented Jan 21, 2014 at 9:43
  • I have the script after the form if that is what you mean? Commented Jan 21, 2014 at 9:48
  • @Kane Yes, that's what I meant Commented Jan 21, 2014 at 10:37

6 Answers 6

2

You have two problems.

  1. Your form is named signup and your global function is named signup. The function is overwritten by a reference to the HTML Form Element Node.
  2. Your submit button will submit the form, causing the browser to leave the page as soon as the JS has finished (discarding all the stored data and probably erasing the console log)

Rename the function and add return false; to the end of your event handler function (the code in the onclick attribute.

Sign up to request clarification or add additional context in comments.

3 Comments

oooh... 'Your form is named signup and your global function is named signup. The function is overwritten by a reference to the HTML Form Element Node.' ... well well well... i not considered this. nice!
Why do I need return false;? also your answer fixed my problem.
@Kane — To prevent "2" from happening.
1

Please rename your function name (signup) or Form Name (signup),

because when you are try to access document.signup......

It'll make a type error like, object is not a function

Try below Code,

<script type="text/javascript">
        var emailArray = [];

    function signup() {
        var theForm = document.forms['signupForm'];
        if (!theForm) {
            theForm = document.signupForm;
        }
            var email = theForm.email.value;
            emailArray.push(email);         

            console.log(emailArray[0]);
        }
</script>


<form name="signupForm">
    <label id="email" for="email" placeholder="Enter your email...">Email: </label>
    <input type="email" name="email" id="email" />
    <br />
    <input type="button" name="submit" id="submit" value="Signup" onclick="signup(); return false;"/>
</form>

1 Comment

Ah! This error stating object is not a function does appear for a split second after clicking.
0

The problem that is given is that the form name is "signup" and the function is "signup()", then the function is never executed (this is better explained in this answer). If you change your form name or your function name everything should work as expected.

Comments

0

try this code :

<form name="test">
    <label id="email" for="email" placeholder="Enter your email...">Email: </label>
    <input type="text" name="email" id="email" onBlur=/>
    <br />
    <input type="button" name="submit" id="submit" value="Signup" onclick="signup()"/>
</form>
<script type="text/javascript" charset="utf-8">
       var emailArray = [];
        function signup(){
            var email = document.getElementById('email').value;
            emailArray.push(email);
            alert('You have now stored your email address');
            window.open('http://google.com');
                    console.log(emailArray[0]);
            return false;
        }
</script>

Comments

0

As suggested in the comments, just change your email variable to:

var email = document.getElementById('email').value;

then just push it to your emailArray

EDIT

You'll need to rename the ID of your label. The reason it's currently not working is because the value being returned is that of the first element with the id of email (which is your label, and undefined).

Here's a Fiddle

1 Comment

I tried this but still no output to the console? not even any errors.
-1

I would propose two improvements to your code:

  1. Put your javascript right after the <form> element in order to be sure that dom element exist in the document
  2. Attach click handler using addEventListener method. Read more here.

    Email:
    var emailArray = []; function signup(){ var email = document.getElementById('email').value; emailArray.push(email); alert('You have now stored your email address'); window.open('http://google.com'); console.log(emailArray[0]); return false; } document.getElementById("submit").addEventHandler('click', signup, false);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.