0

I'm obviously very very new to javascript. What I have is an input with the type number with an minimum of 1 and a maximum of 31. I'm asking the user to select the date they were born. However, I am unsure on how to retrieve the users value. If it was a select box with options I could get it but the requirements for the assignment are to use an input box with the type number. I would appreciate any guidance on how to do this, I've been looking up different sources for 3 hours and i'm sure it's very simple, I'm just not seeing it

this is my html:

   <label for="birth_day">Please select your birthday:</label>
         <input type="number" name="birthday" id="birthday"
         min="1" max="31">

Thanks in advance for any help

secretTina
  • 25
  • 2
  • 11

3 Answers3

0

The value of an input element is available from its value property:

var v = document.getElementById("birthday").value;

It will be a string, even with the input type="number", so you'll want to parse it. This answer goes into several ways you can do that, depending on what behavior you want, although with an input type="number" simply doing var n = +v; is probably sufficient.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Just like any other input value, see this demo

<input type="number" name="birthday" id="birthday"
         min="1" max="31" onblur="alertValue(this)">

function alertValue(thisObj)
{
   alert( thisObj.value ); 
}
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

As you said it IS simple. You can use pure javascript for it or you can use jQuery library.

With JavaScript:

//you get the value by id
var value = document.getElementById("birthday").value;

//or you can get by tag name; element type;
var value = document.getElementsByTagName("input").value;

The first one is more specific, since you can have several input elements on the page.

With jQuery:

 //
 $( "input[name='birthday']" ).val(); 

Check jQuery doc. it is very informative with examples. Selecting Elements with jQuery

Best regards,

amol01
  • 1,823
  • 4
  • 21
  • 35