-2

I have same function that I want to make it based on user input ,

    for(var u = 1; u <= tmp; u++){            
      $('#photo_'+u).change(function(){
            $('#src_'+u).val($(this).val());
      });

Caption: tmp is user input

I try jshint in jsfiddle showing error : "Don't make function withit loop"

how to make that function loop

My question is based in this Link Here

Community
  • 1
  • 1
Mamen
  • 1,322
  • 5
  • 21
  • 44

2 Answers2

2

change id and use class:

 $('.photo').change(function(){
     $(this).find('.src').val($(this).val());
 });

change #photo_... and #src_... to class.

  • $(this) will to point to current .photo element

no need for a loop in this case...

ReNiSh AR
  • 2,782
  • 2
  • 30
  • 42
2
var functionStr="";   
     for(var u=1;u<=tmp;u++){    
     if(u==tmp)   
     {
        functionStr=functionStr+"#photo_"+u; 
     }  
     else  
     {   
        functionStr=functionStr+"#photo_"+u+",";       
     }


     $(functionStr).change(function(){

           $('#src_'+u).val($(this).val());

    });
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
Esha Jain
  • 555
  • 5
  • 16