2

I'm trying to retrieve the value of a f.select form helper in my rails application.

The code for the f.select is (in HAML):

= f.select :level, [["L1 - Super easy", 1], ["L2 - Warm-up", 2], ["L3 - Easy exam questions", 3], ["L4 - Moderate exam questions", 4], ["L5 - Difficult exam questions", 5]], id: "level-AS"

which results in the following html:

<select id="test_level" name="test[level]">

    <option value="1" selected="selected">L1 - Super easy</option>
    <option value="2">L2 - Warm-up</option>
    <option value="3">L3 - Easy exam questions</option>
    <option value="4">L4 - Moderate exam questions</option>
    <option value="5">L5 - Difficult exam questions</option>

</select>

The f.select has an id of "level-AS" and I have managed to target it using

$('#level-AS').select()

and that gives me Object { context: HTMLDocument → new, selector: "#level-AS" } but what I want is for it to return the actual value that has been selected, i.e. "Easy" or "Hard".

I also tried

$('#level-AS').select().val()

and I have also tried

$('#level-AS').val()

but this returns undefined....

1
  • The resulting html (which the javascript works on) has an id of "test_level", not "level-AS". Once you are selecting the right element then $('#<elementID>').val() will give you the currently selected value. (substitute <elementID> for the id of the element you want to get). Commented May 6, 2015 at 9:48

3 Answers 3

4

You should simply do:

$('#level-AS').val()

Or according to your html:

$('#test_level').val()

Sidenote, instead of targeting by id, you could do:

$('select[name="test[level]"]').val()
Sign up to request clarification or add additional context in comments.

7 Comments

that still gives me undefined...I've edited it to include the code for the form_helper I am trying to target
and what if you do: $('#level-A2').val() ? seems you have a typo
Sorry that was from a different page...I've corrected the edit now
well, the js way to retrieve the value is what we suggest you, cant tell much more... whats the resulting html?
I know, which is why I'm so confused as well...I've edited it again to include the html
|
1

I think your problem is that the ID in your f.select is getting lost. I think it's due to how rails is splitting up the arguments into the various sections. Try this:

= f.select :level, [["L1 - Super easy", 1], ["L2 - Warm-up", 2], ["L3 - Easy exam questions", 3], ["L4 - Moderate exam questions", 4], ["L5 - Difficult exam questions", 5]], {}, {id: "level-AS"}

2 Comments

thanks, I couldn't figure out why the id that I wanted wasn't correctly showing up in the html...
The documentation is quite unclear, but the arguments to f.select are field, options to use in the select, hash of special options for the select helper, hash of general other options If you don't wrap the last two in curly braces it thinks all the passed options are part of the third argument. It's an example where ruby's optional braces kind of screw you over if you don't use them.
0

You can call val() method to get value.

$('#level-AS').val()

1 Comment

that gives me undefined still...I have included the code for the form_helper I am trying to target...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.