0

Let's consider the two following line:

mydomain.com?quantity=10

<input type="text" name="quantity" size="1" value="1" />

Is there any way for me to automatically change the value of "quantity" in the URL when a new value is typed by the user (by using the input) ?

Lei Lionel
  • 1,263
  • 13
  • 20
  • Not without another server request. – Adam Konieska May 12 '16 at 21:30
  • what do you want to achieve by doing that? do you want to redirect the user to the URL with the updated quantity? or what? – Phani May 12 '16 at 21:31
  • 1
    You could add a listener to the input and use the methods in this answer to upate the url when the listener fires http://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page – IrkenInvader May 12 '16 at 21:34
  • 1
    Use the [history api](http://stackoverflow.com/a/3340186/978414). – ivann May 12 '16 at 21:52

2 Answers2

0

No. All javascript code affect only the document part of the browser. It can send you to another page where the quantity can be changed though.

Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73
0

Submitting the element value in a form automatically updates the URL with the current value for quantity. If you use the onchange event to trigger submit, the URL will not change until the user clicks outside the input:

<form>
   <input type="text" name="quantity" size="1" value="1"
    onchange="this.form.submit()" />
</form> 

Variations of this could look at keyboard events whilst focus is still with the input element. It remains to be seen if reloading the page would be user friendly and it is not clear if this is indeed what you wish to happen. If you don't want to reload the page see the comment and link from @igwan regarding using the history api (check on MDN for browser support).

Community
  • 1
  • 1
traktor
  • 17,588
  • 4
  • 32
  • 53