javascript - How do set select element array index when it is in array and bind to blur event -


i have array of select dom , input textbox . on blur event of input(of perticular index) want set element in select dom value of same index.

my bind working after each blur event different input box retiurning 0.

i not able set select value i.e. (drop down)as per given index

jsfiddle

my code following.

$(document).ready(function (){     var =1;     $('#addme').on('click', function(){          var  test = '<tr class="employee"><td><input type="text" id="emp_id['+i+']" /></td><td><select id="emp_name['+i+']" '         test += '<option value="-1" >please select </option><option value="e0001" >james smith</option><option value="e0002" >roger sm</option>'           test +='<option value="e0003" >elina lobo</option></select></td></tr>'             $('#addme').after(test);         i++;     });      $(".employee input").live('blur',function(){         var inputindex = $(this).index();         var inputvalue = $(this).val();         alert("input index :" +  inputindex + ' , value ' + inputvalue);          $('#emp_name[inputindex]').each(function(){             if(this.value == inputvalue){                 $('#emp_name[inputindex]').val($(this).val());                      return false;             }             alert("please iput valid value");             $('#emp_name[inputindex]').val('-1');         });     }); });     </script>     <table />     <tr class="employee">         <td><input class="employee" type="text" id="emp_id[0]" /></td>         <td><select id="emp_name[0]" name="emp_name">             <option value="-1" >please select </option>             <option value="e0001" >james smith</option>                      <option value="e0002" >roger sm</option>             <option value="e0003" >elina lobo</option>         </select></td>     </tr>       <input type="button" id="addme" value="add me"/>     </body>  </html>  

thanks in advance

you need delegate events adding elements dynamically on click of button..

so $(".employee input").live('blur',function(){

should this

$(staticcontainer).on('blur',".employee input", function(){ 

staticcontainer element in dom when elements bound element.

closer container better performance.

also have multiple issues code..

it better idea append rows table, selector returns right element inside table.

$('#addme').after(test); 

supposed

$('table').append(test); 

next selector not replace index value

$('#emp_name[inputindex]') 

supposed

$('#emp_name[' + inputindex + ']') 

lastly way accessing indexes wrong.

you trying current input respective other elements .

var inputindex = $(this).index(); 

supposed like

var inputindex = $('input').index(this); // give right value.

check fiddle


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -