0

I've been using forms which upon submit call a function which makes use of the inputted data. So far this has worked fine with text, but switching to date inputs is causing me trouble.

I'm using the following code, but the "startDate", "endDate" values are empty.

<form onsubmit="myFunction()">
  Start Date:
  <input type="date" name="startDate" id="startDate">
  End Date:
  <input type="date" name="endDate" id="endDate">
  <input type="submit">
</form>

<!-- Form to process above date submit -->
<script>
  function myFunction() {

    var locationID = "1";
    var startDate = document.getElementById("startDate").value;
    var endDate = document.getElementById("endDate").value;
    var apiURL = "APIUrl" + locationID + "_" + startDate + "_" + endDate;
    alert("The form was submitted" + apiURL);

    $.get(apiURL, function( data ) {
      $( ".result" ).html( data );
    });
  }
</script>

The alert gives me back the APIUrl, plus the location ID, but blank values for the dates.

Any ideas?

Thanks for your help.

4
  • 1
    It works perfectly well here: JS Fiddle demo; do you have any reported errors in your console (F12 in most browsers)? Commented Jun 12, 2016 at 20:15
  • 1
    I ran your code in jsfiddle and it worked: jsfiddle.net/k15sxbua Commented Jun 12, 2016 at 20:15
  • Thanks for the responses, I'm using Chrome, I'm not seeing anything obvious in the console, but I have a fair amount of code, so I am going to remove anything that Isn't in the above question. Commented Jun 12, 2016 at 20:19
  • 1
    The <form> is still handling the submission itself. It will leave/unload the current page, which will automatically abort the Ajax request. Want html form submit to do nothing Commented Jun 12, 2016 at 20:29

2 Answers 2

1

You didn't specify what browser you are using.

Since you're already using jQuery, I recommend using it to retrieve the field values, i.e.

var startDate = $("#startDate").val();
var endDate   = $("#endDate").val();

That would resolve browser inconsistencies that could play a part in your issue.

Sign up to request clarification or add additional context in comments.

2 Comments

The only inconsistencies I can imagine are in browsers that fail to implement the <input type="date" />, but in that case it defaults back to a type of text. Either way using the value property of the HTMLInputElement is cross-browser compatible.
The OP didn't specify which browser he uses. While I agree that value should not have any inconsistency issues -- if there were no issues then the OP wouldn't have posted his question in the first place.
1

Your code work as expected in Chrome because it supports input date but this is not the case for IE, Firefox and Safari as you can see here : http://caniuse.com/#search=input%20date

It would probably be better if you use a library like JQuery-UI for the datepicker so it can be supported by all browsers

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.