Note that there are some explanatory texts on larger screens.

plurals
  1. POtrouble adding multiple values to array
    text
    copied!<p>In <a href="http://jsfiddle.net/aaronk85/SDCkW/4" rel="nofollow">this fiddle</a> when you select a value it populates all input fields. I am trying to use this same principle in <a href="http://jsfiddle.net/aaronk85/VNSam/14/" rel="nofollow">this fiddle</a> but it is not working. I am not quite sure why? </p> <p>I am not getting any errors but I feel as though the below code is in the wrong spot and is being ignored?</p> <pre><code> jQuery('#newtable select').on('change', function() { var div_id = 'div_'+jQuery(this).attr('id'); var select_val = jQuery(this).find('option:selected').data('value'); console.log(); if( select_val != '' ) { var a = select_val.split(','); var count = 0; jQuery('tr#'+div_id+' .flag').each(function() { jQuery(this).val(a[count]); ++count; }); } else { jQuery('tr#'+div_id+' input.input_text').val(''); } }); </code></pre> <p>What I am trying to acheive in the second fiddle is for when you select 'Ryan $100' it updates the below code.</p> <pre><code> &lt;table id="newtable"&gt; &lt;tr id='div_m2'&gt; &lt;input type="hidden" class='input_text flag' value="0" name="id[]"&gt; &lt;input type="hidden" class='input_text flag' value="0" name="name[]"&gt; &lt;input type="hidden" class='input_text flag' value="0" name="sales[]"&gt; &lt;input type="hidden" class='input_text flag' value="0" name="price[]"&gt; &lt;td colspan="3"&gt; &lt;select id='m2'&gt; &lt;option value="" data-value=""&gt;Choose Salesman&lt;/option&gt; &lt;option value="0" data-value="id_03,Ryan,1,100"&gt;Ryan $100&lt;/option&gt; &lt;option value="0" data-value="id_01,Tom,1,100"&gt;tom $100&lt;/option&gt; &lt;/select&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>to this</p> <pre><code> &lt;input type="hidden" class='input_text flag' value="id_03" name="id[]"&gt; &lt;input type="hidden" class='input_text flag' value="Ryan" name="name[]"&gt; &lt;input type="hidden" class='input_text flag' value="1" name="sales[]"&gt; &lt;input type="hidden" class='input_text flag' value="100" name="price[]"&gt; </code></pre> <p>and when you do this showValues() is also run so that the above data is included in the table I am generating.</p> <p>So my desired output for the table if you choose ryan $100 is.</p> <pre><code> &lt;table id="results"&gt; &lt;tbody&gt; &lt;tr&gt;&lt;th&gt;Rank&lt;/th&gt;&lt;th&gt;Salesman&lt;/th&gt;&lt;th&gt;Products Sold&lt;/th&gt;&lt;th&gt;Total Sale Price&lt;/th&gt;&lt;th&gt;Commission (30% of Total Sale Price + 5 for each sale)&lt;/th&gt;&lt;/tr&gt; &lt;tr data-id="id_03"&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;ryan&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;110&lt;/td&gt;&lt;td&gt;550.00&lt;/td&gt;&lt;/tr&gt; &lt;tr data-id="id_02"&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;Jerry&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;60&lt;/td&gt;&lt;td&gt;300.00&lt;/td&gt;&lt;/tr&gt; &lt;tr data-id="id_01"&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;Tom&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;50&lt;/td&gt;&lt;td&gt;250.00&lt;/td&gt;&lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre>
 

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