2scottish
2scottish

Reputation: 683

Show/Hide Text Field Based on Select Box Selection

In a simple form:

<%= form_for @user do |f| %>
  <%= f.label :source, "How did you find out about us?", :class => "control-label" %>
  <%= f.select(:source, options_for_select([['--  Please select  --',nil],['Source 1','Source 1'], ['Source 2','Source 2'], ['Other','Other']])) %>

  <%= f.label :source_other, "Specify Other Source" %>
  <%= f.text_field :source_other %>
<% end %>

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:

$("#source").change(function() {
  if ($("#source").val()=="Other")
    $("#source_other").show();
  else
    $("#source_other").hide();
});

However, I am not quite understanding how to integrate the JQuery with my form. Could someone please point me in the right direction?

Updated:

Here is the resulting html snippet:

<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
  <label class="control-label" for="user_lead_source">How did you find out about us?</label>
  <select id="user_source" name="user[source]"><option value="">--  Please select  --</option>
    <option value="Source 1">Source 1</option>
    <option value="Source 2">Source 2</option>
    <option value="Other">Other</option>
  </select>

  <label for="user_source_other">Specify Other Source</label>
  <input id="user_source_other" name="user[source_other]" size="30" type="text" />
</form>

Upvotes: 1

Views: 3271

Answers (4)

Ganesh Raju
Ganesh Raju

Reputation: 105

 $(document).on("change", "#user_source", function(){
    var value = $(this).val();
    if (value === "Other"){
      $("#source_other").show();
    }else{
      $("#source_other").hide();
    }
  })

Upvotes: 1

user1683039
user1683039

Reputation: 75

replace

 $("#source") with $("#user_source")

Upvotes: 1

freddydewaleyne
freddydewaleyne

Reputation: 13

It should be $("#user_source") instead!

Rails adds the model name before the attribute. Then it should hide/show $("#user_source_other") element.

Moreover, you must wrap this jQuery JS code in $(document).ready() or $(function(){}); as charlietfl answered before.

Upvotes: 0

charlietfl
charlietfl

Reputation: 171669

I am suspecting that your server code doesn't generate an ID for the element, in which case your selectors are looking for elements with ID's that don't exist

If that is the case either add an ID with your server code so your jQuery ID selectors will work or use name= selectors

$(function(){
    $('input[name="source"]').change(function() {
      $('input[name="source_other"]').toggle(  $(this).val()=="Other" );

    });
});

As long as jQuery code is wrapped in $(document).ready() or $(function(){}); which is shorthand for same, you can place it anywhere in page in a script tag so long as it is after after jQuery library has loaded . Or you can put it in extrnal file that loads after jQuery

Upvotes: 2

Related Questions