Copy nearest select box value of a given class name to following select box of same class name (using JQuery) -


what i'm trying is:

on html sample below, when user clicks "copy above", want jquery search 'up' html structure click location, find previous select box class="colors", copy selected value from select box , use select same value in select box below (also class="colors"). every select box has same set of values.

sample html:

<select class="colors"> <option value="1">red</option> <option value="2">blue</option> <option value="3">green</option> </select>  <a href="#" class="copy-times">copy above</a> <select class="colors"> <option value="1">red</option> <option value="2">blue</option> <option value="3">green</option> </select>  <a href="#" class="copy-times">copy above</a> <select class="colors"> <option value="1">red</option> <option value="2">blue</option> <option value="3">green</option> </select>  <a href="#" class="copy-times">copy above</a> <select class="colors"> <option value="1">red</option> <option value="2">blue</option> <option value="3">green</option> </select> 

i have tried various combinations of .prevall(), can't working.

any appreciated, thanks.

i thought pointless @ first, see in action, see there's use behavior. patience in explaining it.

this seems pretty basic nothing fancy.

  • delegate click event on occurrences of .copy-times
  • prevent .copy-times jumping <a>nchor
  • use variable above (var above) store this (.copy-times) previous sibling (.prev()) class .colors (select.colors) value (.val()).
  • get this (.copy-times) next sibling (.next()) class .colors (select.colors) value (.val()) , set value of previous value (above).

snippet

$('.copy-times').on('click', function(evt) {    evt.preventdefault();    var above = $(this).prev('.colors').val();    $(this).next('.colors').val(above);  });
select,  {    display: block;  }  select {    margin: 0 0 20px 10px;  }  {    margin: 0 0 5px 0;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <select class="colors">    <option value="1">red</option>    <option value="2" selected>blue</option>    <option value="3">green</option>  </select>    <a href="#" class="copy-times">copy above</a>  <select class="colors">    <option value="1">red</option>    <option value="2">blue</option>    <option value="3">green</option>  </select>    <a href="#" class="copy-times">copy above</a>  <select class="colors">    <option value="1">red</option>    <option value="2">blue</option>    <option value="3" selected>green</option>  </select>    <a href="#" class="copy-times">copy above</a>  <select class="colors">    <option value="1">red</option>    <option value="2">blue</option>    <option value="3">green</option>  </select>


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -