javascript - eq(0) is not a valid selector with $.each -


<select class="qty">     <option>1</option>     <option>2</option>     <option>3</option>     <option>4</option> </select> <select class="qty">     <option>1</option>     <option>2</option>     <option>3</option>     <option>4</option> </select> 

i have 2 select tags , want update them array. means changing selected correct value, why in attempt below :eq() not working loop?

selectedarr = [2,3];  $.each(selectedarr,function(i,obj){     console.log(obj);     $('.qty:eq(' + + ')').prop('selected', true); }); 

there's few things wrong here:

  1. $('.qty:eq('+i+')').prop('selected',true) setting selected property on <select> element - not <option> element
  2. you aren't selecting appropriate option array according selectedarr

i this:

var selectedarr = [2, 3];    $.each(selectedarr, function(i, obj) {    $('.qty:eq(' + + ')').val(obj);  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <select class="qty">    <option>1</option>    <option>2</option>    <option>3</option>    <option>4</option>  </select>      <select class="qty">    <option>1</option>    <option>2</option>    <option>3</option>    <option>4</option>  </select>


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -