user3413723
user3413723

Reputation: 12213

Append text to all members of a class jQuery

I have a number of input boxes of class "inputbox". I'd like to append ".00" to the end of each of them. I tried this:

    $('.inputbox').val($(this).val()+".00");

But the $(this) doesn't return any value, so I just get ".00" in all the boxes. Any help on how to get the value from each of those input boxes so I can add to it would be greatly appreciated.

Upvotes: 0

Views: 174

Answers (2)

adeneo
adeneo

Reputation: 318182

val() has a callback, you can use it for exactly things like this, where you need to concantenate to the values of many elements in a collection.

$('.inputbox').val(function(_, val) { return val + ".00" });

Upvotes: 1

Hüseyin Cevizci
Hüseyin Cevizci

Reputation: 919

jQuery selectors return arrays. But val() function can not be used for arrays, it can be used for a single element. So you can use .each() of jQuery:

$('.inputbox').each(function(){
   $(this).val($(this).val()+".00");
});

Upvotes: 0

Related Questions