2

I've read several stackoverflow posts on how to get the value from the datepicker field. I found out that there are two methods to do that:

 var method1 = $('#dateInput').datepicker('getDate'); 
 var method2 = $('#dateInput').val();

However, method2 gives me an empty string. Why is that so? I know that .val() is a method that will get the value of the element.

DEMO: https://jsfiddle.net/Issaki1/wun8Lomg/1/

3
  • what if you do $('#dateInput input').val() ? Commented Jun 15, 2018 at 6:02
  • Hi @RaviMariya, your method works! May I know the explanation behind it? You can add the explanation as your answer and I will accept it Commented Jun 15, 2018 at 6:06
  • I might not be the best person to explain but I see that $('#dateInput') was returning div with no property value where $('#dateInput input') return input with property value Commented Jun 15, 2018 at 6:27

2 Answers 2

3

change your method 2 code like below because in your HTML dateInput as id refers to the div and not actual input text. So you can target input text like below:

var method2 = $('[name="dateInput"]').val();

Or

var method = $('#dateInput input').val();
Sign up to request clarification or add additional context in comments.

Comments

1

This can be done using the below line

var method2 = $('#dateInput input').val();

Reason what i suspect is <div> has the id="dateInput" and the text box has the same id="dateInput". So if we specify which object to consider then it is resolved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.