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?
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?
Share Improve this question asked Feb 3, 2014 at 22:47 user2397282user2397282 3,81815 gold badges49 silver badges98 bronze badges 1- you need to use single quotes. – putvande Commented Feb 3, 2014 at 22:48
2 Answers
Reset to default 12Try:
<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.
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;
});