nimrod
nimrod

Reputation: 5732

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!

Upvotes: 3

Views: 15577

Answers (4)

Stefan Koenen
Stefan Koenen

Reputation: 2337

Maybe you can use this jQuery / AJAX to submit the form without refreshing te page?

http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

else you can use javascript:

document.getElementById('id').value

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074595

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:

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.

Upvotes: 1

Pablo
Pablo

Reputation: 2854

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

Upvotes: 12

remi bourgarel
remi bourgarel

Reputation: 9389

document.getElementById('my_txt_box_id').value

where your text box 's id is "my_txt_box_id"

Does that fit your need ?

Upvotes: 0

Related Questions