Note that there are some explanatory texts on larger screens.

plurals
  1. POShow/Hide Text Field Based on Select Box Selection
    primarykey
    data
    text
    <p>In a simple form:</p> <pre><code>&lt;%= form_for @user do |f| %&gt; &lt;%= f.label :source, "How did you find out about us?", :class =&gt; "control-label" %&gt; &lt;%= f.select(:source, options_for_select([['-- Please select --',nil],['Source 1','Source 1'], ['Source 2','Source 2'], ['Other','Other']])) %&gt; &lt;%= f.label :source_other, "Specify Other Source" %&gt; &lt;%= f.text_field :source_other %&gt; &lt;% end %&gt; </code></pre> <p>I am trying to learn how to use JQuery to only show the "source_other" text field when the value "Other" is selected from the "source" field. From what I've seen online, it looks like I need to use something like this:</p> <pre><code>$("#source").change(function() { if ($("#source").val()=="Other") $("#source_other").show(); else $("#source_other").hide(); }); </code></pre> <p>However, I am not quite understanding how to integrate the JQuery with my form. Could someone please point me in the right direction?</p> <p><strong>Updated:</strong></p> <p>Here is the resulting html snippet:</p> <pre><code>&lt;form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post"&gt; &lt;label class="control-label" for="user_lead_source"&gt;How did you find out about us?&lt;/label&gt; &lt;select id="user_source" name="user[source]"&gt;&lt;option value=""&gt;-- Please select --&lt;/option&gt; &lt;option value="Source 1"&gt;Source 1&lt;/option&gt; &lt;option value="Source 2"&gt;Source 2&lt;/option&gt; &lt;option value="Other"&gt;Other&lt;/option&gt; &lt;/select&gt; &lt;label for="user_source_other"&gt;Specify Other Source&lt;/label&gt; &lt;input id="user_source_other" name="user[source_other]" size="30" type="text" /&gt; &lt;/form&gt; </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