jquery - Can't set the value of input with Javascript - Stack Overflow

I'm trying to change the value of an input field with Javascript.I tried everything, but nothing

I'm trying to change the value of an input field with Javascript.
I tried everything, but nothing seems to works. I tried putting the 5 between quotation marks and using jquery. I also double-checked the array and everything.

Here is the input code:

<input type="number" id="id_[SOME_ID_HERE]" value="0">

and the loop used to update the values.

for (var i = 0; i < shoppingCart.length; i++) {
    var val = shoppingCart[i];
    document.getElementById("id_" + val.substring(3)).value = 5;
}

jsfiddle: /

EDIT: Seems like it doesn't work with type="text" as well...

EDIT2: Thank you everyone who answered. My problem was actually something else.
The input was loaded from another page, and it took time and the for loop I had problem with (see above) was executed before the file was done loading.
All I did was to move the for loop as is to the callback function and it works now.

Thanks anyways!
I really appreciate the help I'm getting in this site! :)

I'm trying to change the value of an input field with Javascript.
I tried everything, but nothing seems to works. I tried putting the 5 between quotation marks and using jquery. I also double-checked the array and everything.

Here is the input code:

<input type="number" id="id_[SOME_ID_HERE]" value="0">

and the loop used to update the values.

for (var i = 0; i < shoppingCart.length; i++) {
    var val = shoppingCart[i];
    document.getElementById("id_" + val.substring(3)).value = 5;
}

jsfiddle: http://jsfiddle/zkTud/

EDIT: Seems like it doesn't work with type="text" as well...

EDIT2: Thank you everyone who answered. My problem was actually something else.
The input was loaded from another page, and it took time and the for loop I had problem with (see above) was executed before the file was done loading.
All I did was to move the for loop as is to the callback function and it works now.

Thanks anyways!
I really appreciate the help I'm getting in this site! :)

Share Improve this question edited Jun 25, 2012 at 12:30 Asaf asked Jun 25, 2012 at 12:08 AsafAsaf 2,0357 gold badges38 silver badges60 bronze badges 1
  • 1 alert() is your always friend: alert(val.substring(3)) – Adi Commented Jun 25, 2012 at 12:12
Add a ment  | 

3 Answers 3

Reset to default 3

The problem is that your call to substring is returning too much of the string, so there are no elements found by getElementById. Change it to this:

for(var i = 0; i < shoppingCart.length; i++) {
    var val = shoppingCart[i];
    document.getElementById("id_" + val.substring(5)).value = 5;
}

Here's an updated fiddle.

The substring method (when called with one argument) returns the characters from the index specified to the end of the string. Since you are specifying index 3, you get "d_1", "d_2" etc. when actually you just want the number.

Alternatively, you could of course change the string to which you append the substring, but I think that would be more confusing to read (not immediately obvious which element will be returned):

document.getElementById("i" + val.substring(3)).value = 5;

demo http://jsfiddle/bY4EV/6/

sustring(3) gives d_1 : How to substring in jquery

hope this helps

code

var shoppingCart = new Array();
shoppingCart[0] = "prod_1";
shoppingCart[1] = "prod_3";
shoppingCart[2] = "prod_2";

for(var i = 0; i < shoppingCart.length; i++) {
    var val = shoppingCart[i];

    $("#id_" + val.substring(5)).val("5");
}

​

Check this, JSFiddle , Updated and corrected your problem.

Code:

var shoppingCart = new Array();
shoppingCart[0] = "prod_1";
shoppingCart[1] = "prod_3";
shoppingCart[2] = "prod_2";

for(var i = 0; i < shoppingCart.length; i++) {
    var val = shoppingCart[i];

    $("#id" + val.substring(4)).val( "5");
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745331335a4622890.html

相关推荐

  • jquery - Can&#39;t set the value of input with Javascript - Stack Overflow

    I'm trying to change the value of an input field with Javascript.I tried everything, but nothing

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信