NimChimpsky
NimChimpsky

Reputation: 47290

How can alter the value of every single input element, by a fixed amount, without using a loop?

I want select every inputbox of type text and transform by a fixed amount, when checkbox is checked, so far I have this. However I wondered if there was a way to do this without iterating over every element ...

    $("#myID").change(function(){

       if($(this).is(':checked')){

           //I can't do this :
           $("input:text").val( $("input:text").val()*4);     

          //I have to do this ?
          $("input:text").each(function(index){
               $(this).val($(this).val()*4);
           });

    });

Upvotes: 2

Views: 67

Answers (1)

lonesomeday
lonesomeday

Reputation: 237975

If you are using jQuery 1.4+, you can use a function callback to calculate the value:

$("input:text").val(function(idx, oldVal) {
    return oldVal * 4;
});

See the API.

Upvotes: 2

Related Questions