Onclick window.location.href with input value

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

Here is how my input is configured:

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

Basically, when the date changes, the date should be added at the end of the URI, and the user should be sent to test.php, where the value will be retrieved. However, the value does not seem to be added.

What is the problem?

+3
source share
2 answers

Try:

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

You do not need to do document.getElementById('datepicker'), since the element you are in is already the one with which you want to get the value.

+10
source

, , onchange. , :

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

, , jQuery, ?

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

...

$("#datepicker").change(function() { 
    window.location.href = 'test.php?Date=' + this.value;
});
+2

All Articles