Question

I have an input and I want the user to be sent to another page with the input's value in the URI so that I can extract it.

This is how my input is set up:

<input name="date" id="datepicker" onchange="window.location.href = 'test.php?Date=' + document.getElementById("datepicker").value;">

Basically, when the date is changed, the date should be added onto the end of the URI and the user should be sent to test.php, where the value will be extracted. However, the value doesn't seem to be added on.

What's the problem?

Was it helpful?

Solution

Try:

<input name="date" id="datepicker" onchange="window.location.href = 'test.php?Date=' + this.value;">

You don't need to do document.getElementById('datepicker') since the element you are in is already the one you want the value from.

OTHER TIPS

The problem is that you're using double-quotes to delimit the attribute value and the string value within the onchange event. Try using single-quotes, like this:

<input name="date" id="datepicker" onchange="window.location.href = 'test.php?Date=' + document.getElementById('datepicker').value;">

And of course, since you've tagged the question with jQuery, why not use it?

<input name="date" id="datepicker">

...

$("#datepicker").change(function() { 
    window.location.href = 'test.php?Date=' + this.value;
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top