Bala Krishnan
Bala Krishnan

Reputation: 25

How to get value from multiple different textbox through javascript

<html>
    <head>
    <script>
        function showUser()
        {
            for (var i = 0; i < 10; i++)
            {
                var internal = document.getElementById("inte[i]").value;   /* but its not working */
                var external = document.getElementById("exte[i]").value;
            }
        }
    </script>
    </head>
    <body>
    <?php 
        for (i = 0; i < 10; i++)
        {
            echo "<td><input type='text' name='internal' id = 'inte[$i]' width='30'/> </td>";
            echo "<td><input type='text' name='external' id = 'exte[$i]' width='30'/> </td>";
        }
        echo "<td><input type='submit' name='submit1' value='Save Marks' width='30'  onclick = 'showUser()' /></td>";
    ?>
    </body>
<html>

i am using the above code but i cant get 10 values of different textboxes how to fetch these value through java script and i want to save it to database kindly help me

i am using the above code but i cant get 10 values of different textboxes how to fetch these value through java script and i want to save it to database kindly help me

Upvotes: 0

Views: 3437

Answers (3)

Richard Christensen
Richard Christensen

Reputation: 2166

When your Javascript for loop runs you are are overwriting your var internal and external each time so after the for loop runs you will only have the last value that was assigned to the variables. You should use an array and push the values of the text boxes into it.

var internal = new Array();
var external = new Array();

for (var i=0; i<10; i++)
{
  internal.push(document.getElementById("inte["+i+"]").value);   /* but its not working  */              
  external.push(document.getElementById("exte["+i+"]").value);
}

Upvotes: 0

David
David

Reputation: 218818

This isn't doing what you think:

getElementById("inte[i]")

That's just a string, the i value doesn't get interpreted into an integer. This would:

getElementById("inte[" + i + "]")

Upvotes: 1

Naveen Kumar Alone
Naveen Kumar Alone

Reputation: 7668

You are not placed i value in getElementById

Replace your

document.getElementById("inte[i]").value;

as

document.getElementById("inte["+i+"]").value;

Make your for loop as

for (var i=0; i<10; i++)
{
  var internal = document.getElementById("inte["+i+"]").value;   /* but its not working  */              
  var external = document.getElementById("exte["+i+"]").value;
}

Upvotes: 1

Related Questions