- Copy selection from one select field to many other select fields with jQuery?
- <select class="offer_1 value_source" name="xnamex">
- <option value="">Please Select</option>
- <option value="xxxxxxx">Text</option>
- </select>
- <button class="copy_select offer_1" type="button" name="Copy" value="">Copy Down</button>
- <select class="offer_1" name="xnamex">
- <option value="">Please Select</option>
- <option value="xxxxxxx">Text</option>
- </select>
- <!-- unknown quantity of selects follow -->
- $(document).ready(function() {
- $('.copy_select').click(function() {
- attr_class = $(this).attr('class');
- attr_class = attr_class.replace('copy_select ', '.');
- $(attr_class).val($(attr_class + ' value_source').val());
- alert(attr_class);
- });
- });
- $('.copy_select').click(function() {
- $selects = $($(this).data('selects'));
- $source = $selects.filter('.value_source');
- $copies = $selects.not('.value_source');
- console.log($selects);
- console.log($source);
- console.log($copies);
- console.log($source.val());
- $copies.val($source.val());
- })
- <select class="offer_1 value_source" name="xnamex">
- <option value="">Please Select</option>
- <option value="a">Text</option>
- <option value="b">Foo</option>
- <option value="c">Bar</option>
- </select>
- <button class="copy_select" data-selects='.offer_1' type="button" name="Copy" value="">Copy Down</button>
- <select class="offer_1" name="xnamex">
- <option value="">Please Select</option>
- <option value="a">Text</option>
- <option value="b">Foo</option>
- <option value="c">Bar</option>
- </select>
- $(".copy_select").click(function() {
- $("select[class^='offer_']").val($(this).prev().val());
- });
- $(document).ready(function() {
- $(".copy_select").click(function() {
- // store value
- var v = $( ".value_source option:selected" ).val();
- // store text
- var t = $( ".value_source option:selected" ).text();
- $("select[class^='offer_']").append(
- "<option value="" + v + "">" + t + "</option>"
- );
- });
- });