javascript - I want to loop through an array and modify attributes - Stack Overflow

Here is my codevar input_buttons = ["#one","#two","#three"]; var substr

Here is my code

var input_buttons = ["#one","#two","#three"]; 
var substr = input_buttons.split(',');
for(var i=0; i< substr.length; i++) 
{
   substr.attr('value', '');
}

Why doesn't this work?

Here is my code

var input_buttons = ["#one","#two","#three"]; 
var substr = input_buttons.split(',');
for(var i=0; i< substr.length; i++) 
{
   substr.attr('value', '');
}

Why doesn't this work?

Share Improve this question edited Aug 24, 2014 at 3:46 AstroCB 12.4k20 gold badges59 silver badges74 bronze badges asked Mar 9, 2011 at 15:12 KeithKeith 26.5k36 gold badges98 silver badges129 bronze badges 1
  • You need to tell us, what your code is it supposed to do, because that doesn't make any sense at all. input_buttons is an array and arrays doesn't have a method split. Strings do have a method split which returns an array and array's don't have a method attr either. – RoToRa Commented Mar 9, 2011 at 15:18
Add a ment  | 

7 Answers 7

Reset to default 4

Your first problem is calling split(',') on an array. However, if you just want to set the values of all those to a blank string you can do:

$('#one,#two,#three').val('');

If you want to set different values you'd need to loop through:

$('#one,#two,#three').each(function() {
  // this == the HTML node (not a jQuery element)
  this.value = someValue; // someValue would set outside
};

You already have an array, there is nothing to split, this only works on strings. You'd also have to pass the ID to jQuery before you can cal attr. In this case val is even better.

var input_buttons = ["#one","#two","#three"]; 
for(var i=input_buttons.length; i--;) {
   $(input_buttons[i]).val('');
}

But shorter would be using the multiple selector:

$('#one, #two, #three').val('');

or if you already have the array, create a string by joining the IDs:

$(input_buttons.join(',')).val('');

I'm wondering why you are calling:

var substr = input_buttons.split(',');

By the nature of your input_buttons, you already have an array. All you should have to do is:

var input_buttons = ["#one","#two","#three"]; 
for(var i=0; i< substr.length; i++) 
{
   $(input_buttons[i]).attr('value', '');
}
var input_buttons = ["#one","#two","#three"]; 
$.each(input_buttons, function(idx, value) {
    $(value).val('');
});

Or even better and shorter:

$('#one, #two, #three').val('');

You could also give those elements a mon class name and then use this:

$('.className').val('');

your array contains just the id but not the actual object try this

var input_buttons = ["#one","#two","#three"]; 

for(var i=0; i< input_buttons.length; i++) 
{
   $(input_buttons[i]).removeAttr('value');
}

input_buttons is already an array - don't split it.

To use .attr you need it to be a jquery object, so call $(input_buttons[i]).attr

Try the following to remove an attribute:

var input_buttons = ["#one","#two","#three"]; 
for(var i=0; i< input_buttons.length; i++) 
{
   $(input_buttons[i]).removeAttr('value');
}

The reason your code does not work is in the overloading of jQuery functions. .attr('value', '') evaluates to .attr('value'), which returns the value of value as opposed to setting it. The reason is that '' evaluates to false.

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信