Stephen Raj
Stephen Raj

Reputation: 41

Get array text filed value via javascript

in my process text filed in array i need to get the value via javascript but my code is not working my code following

<input type="text" id="itemid[]" name="itemid[]" class="span12"/>

and javascript code is

function getstock()
{
 var itemidarr=document.getElementById('itemid[]');
 if(itemidarr!= null)
 {

    alert(itemidarr.length);

 }
}

any other solution for this

Upvotes: 0

Views: 95

Answers (6)

Stephen Raj
Stephen Raj

Reputation: 41

i got the result

function getstock1()
    {
    alert("test");
        var itemidarr = document.getElementsByName('itemid[]');
        for (var i = 0; i < itemidarr.length; i++)
        {
            alert(itemidarr[i].value);
        }

Upvotes: 0

vivek kumar
vivek kumar

Reputation: 19

function getstock() {

var a = []; jQuery.each(this, function(i, field){ a.push($.trim(field.value)); }); return a;

}

can u use this code

Upvotes: 0

Charles-Antoine Fournel
Charles-Antoine Fournel

Reputation: 1783

You can't make an array to fill in an input text, only for input like radio or checkbox.

Text input only accept a single string.

Upvotes: 0

Chris Parsonage
Chris Parsonage

Reputation: 93

you can try something like this below

<input type="text" id="itemid" name="itemid" class="span12"/>
function getstock()
{
    var itemidarr = document.getElementsByName('itemid');
        for(var i = 0; i < itemidarr.length; i++){
            var item=document.getElementsByName('itemid')[i].value;
                if(item!= null)
                {
                  alert(item);
                }

    }
}

Upvotes: 0

Amar
Amar

Reputation: 2243

The id attribute here cannot contain [ ].

Put your textboxex in "fieldset tag" Like:

<fieldset id="field">
//Put you text boxes here<input type='text'>
</fieldset>

Access these by:

document.getElementById("list....").getElementsByTagName("input")[indexoftext];

indexoftext is the text box you wan to choose.

Hope it helps!

Upvotes: 0

Display_name
Display_name

Reputation: 103

The IDs can't contain brackets, these: [], so:

<input type="text" id="itemid1" name="itemname1" class="span12"/>
<input type="text" id="itemid2" name="itemname1" class="span12"/>
<input type="text" id="itemid3" name="itemname1" class="span12"/>

Then you have to loop through the IDs:

function getstock()
{
     for(var i=1; i<=3; i++){
         var itemidarr=document.getElementById("itemid"+i);
         if(itemidarr!= null) alert(itemidarr.length);   
     }
}

Upvotes: 1

Related Questions