0

I have html textboxes, but for somme reason when I hit submit it doesn't show both neither of the 3

<script type="text/javascript">     
    function readBox() {
        var phone = document.getElementById('phone').value;
        var email = document.getElementById('email').value;         
        alert("You typed " + email, + age, + tShirt);
    }
</script>

any ideas why this isn't working?

<form name="readBox" action="/">                        

    <label>Age:</label>                     
    <input type="text" id="age" name="age">

    <label>T-Shirt Size:</label>                        
    <input type="text" id="tShirt" name="tShirt">


    <label>Email Address:</label>                       
    <input type="text" id="email" name="email">

    <br />              
        <input type="submit" style="display: inline-block;" onClick="readBox()">
Harpreet Singh
  • 2,651
  • 21
  • 31

3 Answers3

1

Assuming you are only getting value of "You typed " + email

alert("You typed " + email + age + tShirt);

You had many commas. Also, you don't have any element with ID phone, and tShirt variable is not defined.

Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
0

I think it won't go to the function readBox only. Your form name and function name to be executed onclick are same. That's why it's creating problem.

Change your form name and it should go to that function.

Here I have something which worked for me:- HTML

<form action="/">   
                <label>Age:</label>                     
                <input type="text" id="age" name="age">

                <label>T-Shirt Size:</label>                        
                <input type="text" id="tShirt" name="tShirt">


                <label>Email Address:</label>                       
                <input type="text" id="email" name="email">

                <br />              
                <input type="submit" style="display: inline-block;" onclick="readBox()">
              </form>

Javascript

function readBox() {
        var age = document.getElementById('age').value;
        var email = document.getElementById('email').value;         
        alert("You typed " + email +" "+ age);
    }
Mritunjay
  • 25,338
  • 7
  • 55
  • 68
  • [Here](http://stackoverflow.com/questions/9158238/why-js-function-name-conflicts-with-element-id) is a question about conflict between form or it's element's name & function to execute. – Mritunjay Jun 28 '14 at 07:28
0
  1. you must remove comma in alert.
  2. remove action in form
  3. change function name of script

    function read(){
    var email = document.getElementById('email').value;
    var age = document.getElementById('age').value;
    var tShirt = document.getElementById('tShirt').value;       
    
    alert("You typed " + email +","+ age+"," + tShirt);
    
    }
    

`

<form name="readBox"> 
    <label>Age:</label>                     
    <input type="text" id="age" name="age">
    <label>T-Shirt Size:</label>                        
    <input type="text" id="tShirt" name="tShirt">
    <label>Email Address:</label>                       
    <input type="text" id="email" name="email">

    <br />              
    <input type="button" style="display: inline-block;" onClick="read()" value="submit">
narin
  • 1
  • 1