Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get textfield value without submit (html)

Can I get the value of a text field in html without a form and without submit?

I currently have a form on my website and inside this form I have a textfield. Unfortunately I can't have 2 forms at the same time (but I actually need two seperate actions), so I am looking for a way to get the value of the textfield without a submit.

All help is appreciated!

like image 994
nimrod Avatar asked Sep 04 '26 14:09

nimrod


2 Answers

Try document.getElementById("textfield-id").value

like image 53
Pablo Avatar answered Sep 06 '26 03:09

Pablo


You can get a reference to the text field element (via getElementById or any of several other DOM mechanisms), and then look at its value property.

Separately, if your form element has a name, and your input element has a name, you can access them via window: window.formName.textFieldName.value I don't think this is covered by an active spec (yet), but it probably will be at some stage (probably by an evolution of this draft spec or a replacement of it) as it's nearly universally supported.

References:

  • DOM2 Core
  • DOM2 HTML Bindings
  • DOM3 Core
  • HTML5 APIs

Alternately:

...I can't have 2 forms at the same time (but I actually need two seperate actions).

You can have two submit buttons with the same name but different values, and differentiate between which one was clicked on the server (the value for the one that was clicked will be sent). So if you have two submits called "command" and one has the value "Foo" and the other "Bar", clicking the "Foo" button sends command=Foo to the server along with the various form fields, whereas clicking the "Bar" button sends command=Bar to the server with the other fields.

like image 35
T.J. Crowder Avatar answered Sep 06 '26 02:09

T.J. Crowder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!