0

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?

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. Commented Mar 9, 2011 at 15:18

7 Answers 7

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

Comments

2

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('');

Comments

2

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

2 Comments

Strings don't have a methods attr.
@RoToRa - Doh. I always make that stupid error. Thank you for the correction. Fixed.
1
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 common class name and then use this:

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

Comments

1

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

Comments

0

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

Comments

0

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.

3 Comments

Thanks, never knew about removeAttr, but my problem persits with your code. It's not the substr.removeAttr('value'); or substr.attr('value', ''); I feel but probably a syntax error?
substr is not a jquery object. needs to be initialized
substr is nowhere defined in your code. And of course .attr('value', '') should set the value of value to an empty string.

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.