jquery - How do I change the colour of all select2 elements in a specific row -


i using select2, change back-ground colour of of select elements in table. however, jquery code running, changes colour of first select input. tips fixing issue?

my html looks like

<table width="100%" class="table table-striped table-bordered table-hover" id="tb2">    <thead>       <tr>         <th>name</th>         <th>type of student</th>         <th>thesis/ project/ research</th>       </tr>    </thead>    <tbody>       <tr class="table_row">          <td><input class="form-control alert-danger" type="text" name="a"/></td>          <td><select class="form-control alert-danger"">              <option value="">type</option>              <option>bachelors</option>              <option>masters</option>              <option>doctoral</option>              <option>postdoctoral</option>              </select></td>           <td><select class="form-control">               <option value="">select</option>               <option>thesis</option>               <option>project</option>               <option>research</option>               </select></td>        </tr>     </tbody>  </table> 

and jquery code looks like:

$(document).ready(function () {     $('select').select2({         placeholder: "select",         allowclear: true     });     $('#tb2 tbody tr').find("select").data("select2").$container.find('.select2-selection').css("background-color", "rgb(10,10,10)"); }); 

the jquery successful in making first select statement have proper background, not affect second. need make select elements in entire row have background of rgb(10,10,10). have other select inputs on other parts of page, don't want affect background, need affect tr.

your issue in line:

$('#tb2 tbody tr').find("select").data("select2")....... 

the .data("select2") returns first value , not array of elements want change background color.

you can use .each() iterate on each select tag.

$('#tb2 tbody tr').find("select").each(function(idx, ele) {    $(ele).data("select2").$container.find('.select2-selection')                      .css("background-color", "rgb(10,10,10)"); }); 

the snippet:

$('select').select2({      placeholder: "select",      allowclear: true  });  $('#tb2 tbody tr').find("select").each(function(idx, ele) {     $(ele).data("select2").$container.find('.select2-selection')             .css("background-color", "rgb(10,10,10)");  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />  <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>      <table width="100%" class="table table-striped table-bordered table-hover" id="tb2">      <thead>      <tr>          <th>name</th>          <th>type of student</th>          <th>thesis/ project/ research</th>      </tr>      </thead>      <tbody>      <tr class="table_row">          <td><input class="form-control alert-danger" type="text" name="a"/></td>          <td><select class="form-control alert-danger"">              <option value="">type</option>              <option>bachelors</option>              <option>masters</option>              <option>doctoral</option>              <option>postdoctoral</option>              </select></td>          <td><select class="form-control">              <option value="">select</option>              <option>thesis</option>              <option>project</option>              <option>research</option>          </select></td>      </tr>      </tbody>  </table>


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -