onclick window.location.href with input value

user2397282 picture user2397282 · Feb 3, 2014 · Viewed 87.3k times · Source

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?

Answer

putvande picture putvande · Feb 3, 2014

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.