Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Simple and Clean </h2> <p>Not sure how I missed the obvious here.</p> <p>jQuery</p> <pre><code>var updateDiv = function(divClass) { ... $(divClass, row).append(values); } $('tr:has(input)').each(function(){ updateDiv('.hist'); }); $('tr:has(textarea)').each(function(){ updateDiv('.something-else'); }); </code></pre> <p>.</p> <h2>Global Variable Method</h2> <p>You could assign global variables with the class name. By defining the variable before each <code>.each()</code> the <code>updateDiv</code> function uses a different class name. </p> <p>jQuery</p> <pre><code>var updateDiv = function() { ... $(window.divClass, row).append(values); } window.divClass = '.hist'; $('tr:has(input)').each(updateDiv); window.divClass = '.something-else'; $('tr:has(textarea)').each(updateDiv);​ </code></pre> <p>.</p> <h2>HTML5 Data Method</h2> <p>You could assign values as data objects to the elements which are being called. I also cleaned up some of your selectors and jQuery redundancies. </p> <p>Fiddle: <a href="http://jsfiddle.net/iambriansreed/KWCdn/" rel="nofollow">http://jsfiddle.net/iambriansreed/KWCdn/</a></p> <p>HTML</p> <pre><code>&lt;table&gt; &lt;tr data-update=".hist"&gt; &lt;td&gt;AutoI&lt;/td&gt; &lt;td&gt; &lt;input type="text" name="autoIH_complaint" id="autoIH_complaint"&gt;&lt;/td&gt; &lt;td&gt;&lt;input class="NA" type="checkbox" name="autoINA" id="autoINA" value="N/A"&gt;&lt;/td&gt; &lt;td&gt;&lt;div class="hist"&gt;&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>jQuery</p> <pre><code>var updateDiv = function() { var row = this, values = ""; $('input:text,textarea,:radio:checked,:checkbox:checked', this).each(function() { if (this.value != "" &amp;&amp; this.value != null) { if (values != "") values = values + "," + this.value; else values += this.value; } }); if (values != "") { if (values.substring(0, 1) == ",") values = values.substring(1) + "&lt;br&gt;"; else values = values + "&lt;br&gt;"; } $(row.data('update'), row).append(values); } $('tr:has(input)').each(updateDiv); $('tr:has(textarea)').each(updateDiv);​ </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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