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

  • possible duplicate of [Javascript get input text value](http://stackoverflow.com/questions/11563638/javascript-get-input-text-value) – Felix Kling Feb 21 '14 at 10:21
  • HTML Ids can't contain brackets (`[]`). http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html – yoavmatchulsky Feb 21 '14 at 10:21
  • I don't know what you intended with `itemidarr.length`, but DOM elements don't have a `length` property. – Felix Kling Feb 21 '14 at 10:21
  • @yoavmatchulsky: Yes they can: http://dev.w3.org/html5/markup/global-attributes.html#common.attrs.id. – Felix Kling Feb 21 '14 at 10:22
  • Felix Kling:any other solution for this progress – Stephen Raj Feb 21 '14 at 10:30
  • @FelixKling it looks like that's the problem though. – yoavmatchulsky Feb 21 '14 at 10:30
  • @yoavmatchulsky: I doubt it: http://jsfiddle.net/352bZ/ – Felix Kling Feb 21 '14 at 10:34
  • *"any other solution for this progress"* You haven't even explained what the problem is. *What* exactly does not work? What do you expect the code to do and what does it do? You said "it doesn't work", but we don't even know what you expect it do to. If you expect the code to brew coffee, well, them it's obvious that "it doesn't work". – Felix Kling Feb 21 '14 at 10:36
  • @FelixKling k, you win. I thought it could be an old DOCTYPE issue, but i'm guessing modern browsers know how to overcome it. – yoavmatchulsky Feb 21 '14 at 10:45
  • @yoavmatchulsky: Yeah, I think there never was really a problem with that (maybe in really old browsers). – Felix Kling Feb 21 '14 at 10:46
  • @FelixKling I'm used to rails automatically changing `item[x]` names to `item_x` ids – yoavmatchulsky Feb 21 '14 at 10:48
  • The only advice I can give you is to [learn how to](http://www.creativebloq.com/javascript/javascript-debugging-beginners-3122820) [**debug** JavaScript](https://developers.google.com/chrome-developer-tools/docs/javascript-debugging) and poke around the code yourself. – Felix Kling Feb 21 '14 at 11:02

6 Answers6

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);   
     }
}
Display_name
  • 103
  • 1
  • 1
  • 7
  • Yes they can: http://dev.w3.org/html5/markup/global-attributes.html#common.attrs.id. – Felix Kling Feb 21 '14 at 10:36
  • Thanks, Felix. Guess they can. – Display_name Feb 21 '14 at 10:39
  • 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 :) http://www.php.net/manual/en/faq.html.php#faq.html.arrays – Felix Kling Feb 21 '14 at 10:44
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.

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
Charles-Antoine Fournel
  • 1,713
  • 1
  • 25
  • 37
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!

Amar
  • 2,171
  • 1
  • 13
  • 16
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);
                }

    }
}
0

function getstock() {

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

}

can u use this code

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);
        }
  • 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. – Baby Groot Feb 26 '14 at 12:45