You are viewing limited content. For full access, please sign in.

Question

Question

Get text value or index of select field in Forms, current code keeps returning the wrong values.

asked on September 6, 2017

I can't seem to get the proper text value or index of a select field in Forms. As always with Javascript there is 10 different ways to do the same thing, but I am using every example I can find on Google and it always returns the number 1 for both the first and second value.

So here is what I am currently using

var selection = $(this).find('.points option:selected').val();
         alert(selection);

or

var selection = $(this).find('.points select').val();

They both return incorrect values. The drop downs are set to the option "Select" which is the first index. The value returned in the alert is "1" when the drop down is showing "Select"

The second option in the drop down is "1" and when they choose "1" the alert also says "1". How can both "Select" and "1" = "1"?

If you select "2" then it returns "2", which makes sense.

The points inputs are just basic drop down fields being populated like this


points.empty().append('<option value="1">Select</option>');
        for (j = 1; j <= max; j++)
         {
         points.append('<option value="'+j+'">'+j+'</option>');
         }

 

 

0 0

Answer

SELECTED ANSWER
replied on September 6, 2017

Hey,

So the reason select and 1 return the same value is because you have set the value to both as 1.

<option value="1">Select</option>

to fix it you can do one of the following:

1) <option value="-1">Select</option>; select will now return -1

2) $(this).find('.points select option:selected').text(); this will return their label not their value.

 

Hope that helps

0 0
replied on September 7, 2017

Oh I forgot about the value= part, not sure why I have that in there to begin with since the text value is already in between the brackets. Also pulling the text with .text() would have worked had I known that method. Usually text comes across with .val()

Thanks for your help! I will try this out.

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.