Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery need automatically fill only one input field (row) based on value in other field (autocomplete); all other fields must remain unchanged
    primarykey
    data
    text
    <p>This <a href="http://af-design.com/blog/2010/05/12/using-jquery-uis-autocomplete-to-populate-a-form/" rel="nofollow">http://af-design.com/blog/2010/05/12/using-jquery-uis-autocomplete-to-populate-a-form/</a> script automatically fills state and zip, based on city. </p> <p>There is one row with id like id="city". If I want additional rows, I must use something like id="city1", id="city2" etc.</p> <p>In javascript code then may use something like</p> <p><code>$("#city").val(ui.item.city);</code></p> <p><code>$("#city1").val(ui.item.city1);</code></p> <p>This is not ok, if I have many rows.</p> <p>So I trying to change. Below is changed code</p> <pre><code>$(document).ready(function(){ var ac_config = { source: "__demo_cities.php", select: function(event, ui){ $('[id^="city"]').val(ui.item.city); $('[id^="state"]').val(ui.item.state); $('[id^="zip"]').val(ui.item.zip); }, minLength:1 }; $('[id^="city"]').autocomplete(ac_config); }); </code></pre> <p>HTML</p> <pre><code>&lt;input type="text" name="city[]" id="city" value="" /&gt; &lt;input type="text" name="state[]" id="state" value="" /&gt; &lt;input type="text" name="zip[]" id="zip" value="" /&gt; &lt;br&gt; &lt;input type="text" name="city[]" id="city1" value="" /&gt; &lt;input type="text" name="state[]" id="state1" value="" /&gt; &lt;input type="text" name="zip[]" id="zip1" value="" /&gt; </code></pre> <p>If I enter something in <code>city</code> script automatically fills <code>id="state"</code>, <code>id="zip"</code> (that is ok), but it also automatically fills <code>id="city1"</code>, <code>id="state1"</code> and <code>id="zip1"</code> (that is not necessary).</p> <p>Need behavior: if enter something in <code>id="city"</code>, automatically fills only <code>id="state"</code> and <code>id="zip"</code> and all other fields remains blank/unchanged; if enter in <code>id="city1"</code>, automatically fills only <code>id="state"</code> and <code>id="zip"</code> and all other fields remains blank/unchanged.</p> <p>Based on @JNF advice there is one code that works </p> <pre><code>$(document).ready(function(){ var ac_config = { source: "__demo_cities.php", select: function(event, ui){ $(this).closest(".myForm").find('[id^="city"]').val(ui.item.city); $(this).closest(".myForm").find('[id^="state"]').val(ui.item.state); $(this).closest(".myForm").find('[id^="zip"]').val(ui.item.zip); }, minLength:1 }; $('[id^="city"]').autocomplete(ac_config); }); </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.
 

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