0

Ok so I am working on a project that was outsourced and now I get to jump in and fix everything before it gets pushed to production. I am currently trying to modify a line that creates a select form field. I read up on some of the different form helper methods to use in ruby, and over some of the available options for the method.

My problem is I can't seem to figure out how to get a default value to be displayed inside the select element without having it show up in the drop down options. This is what the line currently looks like.

<%= select :profile, :sexual_interest, SEXUAL_INTEREST_ARRAY, {:prompt => 'sexuality'}, {:class => 'selectFull'} %>

I tried changing some of the options using :default => 'sexuality' and then tried :disabled => 'sexuality'. But I seem to only be able to remove the option entirely from the select element or have it as the first value in the drop down list and being displayed as the default value.

If you are just having issues setting the default value of your select field using a ruby form helper, reference this post selecting a default for option field

If any one could give me some insight as to how to fix this issue I would greatly appreciate it. Any ideas would help, Thanks for reading.

-Alan

Community
  • 1
  • 1
Alan DeLonga
  • 454
  • 1
  • 10
  • 27
  • 1
    I don't think this is possible in Html so it would not be in Rails. – WispyCloud Oct 31 '12 at 20:21
  • 1
    Looks like you want an advanced behaviour, might be done using JS. Although I don't understand why a standard default value is not OK? – Anthony Alberto Oct 31 '12 at 20:36
  • Basically its a drop down selector where they want the title for the drop down to be displayed by default, but don't want it to be an available selection in the drop down menu. – Alan DeLonga Oct 31 '12 at 21:10
  • You'll have to add the defaul value to the list and the first time the user clicks on the select you'll have to remove it using JS – felipeclopes Oct 31 '12 at 21:10
  • That does seem like a legitimate solution, the only issue I can see that might arise is if the user clicks for the drop down menu and doesn't actually make a selection. The default value will no longer be displayed and the new first value will be displayed. – Alan DeLonga Oct 31 '12 at 21:14

1 Answers1

1

It is not possible to do that in HTML, so you'll have to use another approach.

Possible solution

Suppose you have:

<select name="selectBox" id="selectBox">
    <option value="default">default</option>
    <option value="option1">option1</option>
    <option value="option2">option2</option>
    <option value="option3">option3</option>
    <option value="option4">option4</option>    
</select>

Then your javascript will looklike this:

$("#selectBox").change(function(){
   $("#selectBox option[value='default']").remove();
}​);

The example on jsFiddle

Selectors

$('#selectBox option[value="SEL1"]')

For an index:

$('#selectBox  option:eq(1)')

For a known text:

$('#selectBox option:contains("Selection 1")')

Hope it helps you!

felipeclopes
  • 4,010
  • 2
  • 25
  • 35
  • Thanks for your comment. I get the overall idea, and that should work for the most part. The issue is the ruby form select helper is being used so I am not actually setting up the values. Because of this there is nothing set for the value for the default... So the html actually just looks like this is there a way to delete the option that has the .val() == defalut? instead of the .value()? – Alan DeLonga Oct 31 '12 at 21:36
  • I put in $(document).ready(function() { $("#profile_gender").change(function(){ $('#profile_gender option:contains("gender")').remove(); }​); } and still getting no change – Alan DeLonga Oct 31 '12 at 21:55
  • Sure, I mean it didn't actually work in my implementation but I guess it could help someone else. And your answer is clear. – Alan DeLonga Nov 01 '12 at 00:06