Jakub Zak
Jakub Zak

Reputation: 1232

input type date, jQuery val() returns element tag and not value

I have a simple input tag:

<input type="date" class="basic_txt" name="date_birth" id="npdl_dBirth" value=""/>

And I need to assign the value of this input to a variable:

ndpl_dBirth = $('#npdl_dBirth').val();

For some reason this is returning whole tag in a console when I try to print the variable out:

console.log(npdl_dBirth);

is returning this:

<input type="date" name="date_birth" id="r_date_birth" value>

I must be missing something really obvious, but can't find out what. Anyone any ideas? Thx in advance.

Thank you for your help, just found my mistake, simply misspelled the variable. Thx anyway

Upvotes: 3

Views: 24977

Answers (2)

Rohan Kumar
Rohan Kumar

Reputation: 40639

I think you should change you variable name, it should be different from your html element id

console.log($('#npdl_dBirth').val());

Will work fine in your case

or

var nb=$('#npdl_dBirth').val();
console.log(nb);

Upvotes: 2

Musa
Musa

Reputation: 97727

Some browsers use the id of elements as global variables, so since npdl_dBirth is the id of the input it will be set as a global variable referencing that variable. Try using a different variable name.

Also your variable is ndpl_dBirth but you try to print npdl_dBirth

Upvotes: 9

Related Questions