0

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

12

6 Answers 6

1

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);   
     }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Felix. Guess they can.
Also (this is something you might not know when you are not working with PHP), the [] in the fields names are important. It lets PHP know to treat multiple elements with the same name as an array. It's strange, but that's the way it is :) php.net/manual/en/faq.html.php#faq.html.arrays
0

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!

1 Comment

0

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);
                }

    }
}

5 Comments

Why should the OP try this?
OP should try this as the first line should get and array of itemid. then loop them all grabbing the value of each one and alerting it
You should explain this in your answer. Also, why did you remove the []? They are essential for processing the fields in PHP. php.net/manual/en/faq.html.php#faq.html.arrays
sorry very new to this :( trying to be help full. only removed the [] for this but yes they are needed for php
@hen Raj sorry i missed of some "s"
0

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.

Comments

0

function getstock() {

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

}

can u use this code

2 Comments

i had change my code what u mention like that but i didn't get
or try this $('input[name="itemid[]"]').each(function(){ alert($(this).val()); });
0

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);
        }

1 Comment

If this is an answer, you can accept your own answer. Or if it is a part of your question, you can simply edit your question.

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.