views:

42

answers:

1

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

    });
+2  A: 

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.

lonesomeday