Paul
Paul

Reputation: 185

I am getting undefined when trying to return the date value from an input field

JAVASCRIPT

I am just trying to returen the value of the date selected but it's coming back undefined.

var getDate = function() {              
    var bdinput = $('#bd.bdinput').val();
    var _this = bdinput;                    
    console.log(_this);

};

$("button.bdsubmit").click(function() {                         
    console.log(getDate());
})

HTML

<form id="bd">                  
<input name="BirthDate" class="bdinput" type="date" />
<button class="bdsubmit"  type="submit">Submit Date</button>

Upvotes: 1

Views: 4096

Answers (2)

Paul
Paul

Reputation: 185

It was a simple mistake.

var bdinput = $('#bd.bdinput').value;

should be:

var bdinput = $('#bd. bdinput').val();

Upvotes: 0

You need .val() to get value of input not .value

var bdinput = $('#bd .bdinput').val();
                 // ^ also need space here

$('#bd .bdinput') will select element with class bdinput inside element with id bd.

$('#bd.bdinput') will select a element with id 'bd' and class bdinput (both id and class in one element)


If you want to do it with .value

var bdinput = $('#bd .bdinput')[0].value;

Or

var bdinput = $('#bd .bdinput').get(0).value;

Upvotes: 3

Related Questions