Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Option Value return Integer by default

Is it has any way to get value from Select Option as Integer by default ?

from select here

<select id="lesson_per_class" >
 <option value="">&nbsp;</option>
 <option value="1">1 Lesson</option>
 <option value="2">2 Lessons</option>
 <option value="3">3 Lessons</option>
 <option value="4">4 Lessons</option>
</select>

When I tried to get their value to calculate, I found that it return as String and can't use to calculate here

var lesson_per_class = $('#lesson_per_class').val();
alert('value is ' + ( 10 + lesson_per_class )); 
// it give result as 10x instead of 1x 

currently, the way I solved is parseInt(), but I need to know, is it has anyway to get it as Integer by default?

like image 854
Sruit A.Suk Avatar asked Jul 15 '15 15:07

Sruit A.Suk


People also ask

Can option value be integer?

The Option type is a zero-based enumerator type, which means that the option values are assigned to sequential numbers, starting with 0. You can convert option data types to integers.

How do I select the default Select option?

The option tag contains the value that would be used when selected. The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.

How do you get the selected value of an option?

The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.

Can Options value string?

Yes, it is a string type, and could have any value.


2 Answers

From the spec you can see the value is a DOMString so no, you cannot get an integer by default.

http://www.w3.org/TR/html5/forms.html#the-option-element

For Java and ECMAScript, DOMString is bound to the String type because both languages also use UTF-16 as their encoding.

[NamedConstructor=Option(optional DOMString text = "", optional DOMString value, optional boolean defaultSelected = false, optional boolean selected = false)]
interface HTMLOptionElement : HTMLElement {
           attribute boolean disabled;
  readonly attribute HTMLFormElement? form;
           attribute DOMString label;
           attribute boolean defaultSelected;
           attribute boolean selected;
           attribute DOMString value;

           attribute DOMString text;
  readonly attribute long index;
};
like image 64
mccainz Avatar answered Sep 21 '22 10:09

mccainz


No, there isn't - val() always returns a string. You have to use parseInt() as you already found out yourself.

There is an index() method, which might work in this case (because the first option has index 0):

$('#lesson_per_class option:selected').index()
like image 44
Glorfindel Avatar answered Sep 21 '22 10:09

Glorfindel