Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get my Onchange function to work more than once?
    primarykey
    data
    text
    <p>I have a form where I've got a button that will add a set of form fields when you click it. One of the form fields is a dropdown menu and I have a function so that when a certain option is picked from the dropdown menu, it populates another form field. So far, it works on the original set of form fields that are shown when the page first loads but it won't work for the set of form fields that gets added. How do I get it to work on both? Here's my javascript:</p> <pre><code>&lt;script type='text/javascript' src='http://code.jquery.com/jquery-1.5.2.js'&gt; &lt;/script&gt;&lt;script type='text/javascript'&gt; //&lt;![CDATA[ $(function () { $('#add').click(function () { var p = $(this).closest('p'); $(p).before('&lt;p&gt; &lt;b&gt;Quantity &amp; Description:&lt;\/b&gt;&lt;br&gt;&lt;textarea name="QuantityAndDescription" rows="10" cols="60"&gt;&lt;\/textarea&gt;&lt;br&gt;&lt;b&gt;Fabric Source:&lt;\/b&gt; &lt;select name="FabricSource" onchange="changevalue(this.value)"&gt;&lt;option value=""&gt;Select Fabric Source&lt;\/option&gt;&lt;option value="JEC"&gt;JEC Fabric&lt;\/option&gt;&lt;option value="Other"&gt;Other Fabric&lt;\/option&gt;&lt;/select&gt;&lt;br&gt;&lt;b&gt;Style# &amp; Name:&lt;\/b&gt; &lt;input type="text" name="Style#andName"&gt;&lt;br&gt;&lt;b&gt;Fabric Width:&lt;\/b&gt; &lt;input type="text" name="FabricWidth"&gt;&lt;br&gt;&lt;b&gt;Repeat Information:&lt;\/b&gt; &lt;input type="text" name="RepeatInfo" size="60"&gt;&lt;input type="hidden" name="COM Required" /&gt; &lt;\/p&gt;&lt;br&gt;'); return false; }); }); function changevalue(val) { if (val == "JEC") document.comform.RepeatInfo.value = "Per JEC Pattern"; } //]]&gt; &lt;/script&gt; </code></pre> <p>Edit:</p> <p>I got it to work using: <code>function changevalue(val) { if(val=="JEC") $("form#comform [name='RepeatInfo']").val("Per JEC Standard"); }</code> instead of what I had before. But now the problem is that when you select the triggering option, ALL the "RepeatInfo" form fields get populated with "Per JEC Standard", not just the one in the same set of form fields as the triggered selection. How can I get this function to work for the added sets of form fields but not have ONE single selection change populating ALL of the "RepeatInfo" form fields in every set?</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.
    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