Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first means I've come up with is:</p> <pre><code>function change(fld, id) { var opt = fld.selectedIndex; if (fld[opt].value != ' ') { var sel = document.getElementById(id); for (var i = sel.options.length - 1; i &gt; -1; i--) { if (fld[opt].value == sel[i].value) sel[i].selected = true; } } } $('select').change( // moving away from intrusive in-line JavaScript function() { var changeTarget; // used to determine which select to change if (this.id == 'make') { changeTarget = 'model'; } else { changeTarget = 'make'; } change(this,changeTarget); }); $('#add').click( function() { $('#table tbody tr:last').clone(true).appendTo($('#table')).find('input,select').each( function() { var attr = this.id.replace(/\d/, ''); // gets the text of the id attribute var length = $('#table tbody tr').length; // finds what number the new attributes should be if ($(this).is('input')){ // select elements don't need their name to be changed $(this).attr({ 'id': attr + length, 'name': attr + length }); } else if ($(this).is('select')){ $(this).attr('id',attr + length); } }); });​ </code></pre> <p><a href="http://jsfiddle.net/davidThomas/ef3eg/3/" rel="nofollow">JS Fiddle demo</a>.</p> <hr /> <p><strong>Edited</strong> to remove the hard-coded 'make'/'model' choice in the <code>select</code> <code>change()</code> event-handler to the following:</p> <pre><code>$('select').change( function() { var changeTarget = $(this) .closest('tr') .find($('select')) .not($(this)) .attr('id'); change(this,changeTarget); }); </code></pre> <p><a href="http://jsfiddle.net/davidThomas/ef3eg/4/" rel="nofollow">JS Fiddle demo</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload