Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  101] [ 2]  / answers: 1 / hits: 25817  / 9 Years ago, wed, july 15, 2015, 12:00:00

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?


More From » html

 Answers
15

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;
};

[#65798] Monday, July 13, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
turnerf

Total Points: 620
Total Questions: 101
Total Answers: 109

Location: French Polynesia
Member since Tue, Jul 7, 2020
4 Years ago
;