onclick window.location.href with input value

88,527

Solution 1

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.

Solution 2

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;
});
Share:
88,527
user2397282
Author by

user2397282

Updated on July 24, 2022

Comments

  • user2397282
    user2397282 almost 2 years

    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?

  • putvande
    putvande over 10 years
    Don't forget to accept the answer if you are happy with the solution.
  • user2397282
    user2397282 over 10 years
    I will, I just have to wait 10 minutes because you were so quick!