5

I want to create a input textbox that does not require a <input type="text"> form element. The user can still type text into this textbox and the value can still be retrieved using Javascript/jQuery. The reason for doing this is because this textbox will be in a form that upon submit, all form input values will be sent to the server side for processing, and there is this textbox that I dont want its values to be automatically send to server side.

Problem: What is the best way to create such a textbox without relying on HTML form elements input?

2
  • Create the input in another form. Commented Nov 13, 2011 at 13:51
  • Remove the borders and outline of the text input and put it in a div as an only child. Commented Jul 26, 2021 at 8:36

6 Answers 6

9

Just don't give the <input type="text"> tag a name attribute and it won't submit. Here is an example:

<input type="text" id="myinput" value="you will not see this submitted" />
Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct answer to OPs problem. Creating a textbox by not using an input tag is silly.
8

You could use a contenteditable div, too.

From the MDN:

<!DOCTYPE html>
<html>
  <body>
    <div contenteditable="true">
      This text can be edited by the user.
    </div>
  </body>
</html> 

http://html5demos.com/contenteditable

2 Comments

+1 This is what I would have said too, but be aware that this doesn't work in all browsers.
It works on all major desktop browsers. Mobile support is lacking, though.
1

if you create dynamic <input type="text"> by jQuery - it WILL post back its values.

What do you care if its values are posted back ? just dont do anything with them in the server side.

or ,

before submit - change the type from input to label. ( + save values)

or ,

if youll add the input OUTSIDE the FORM element - it wont submit.

Comments

1

you can use a textarea instead of input. then apply stlying with css. also you can use your input tag, just keep it outside the <form></form> then it won't be send over to the server.

Comments

1

just create the input, and don't give it a name or id.

1 Comment

You can still give it an id attribute. It will only get submitted if it has a name attribute.
1
$("#form").bind('submit', function() {
  $(this).children('input[name=excludethisfield]').attr('disabled', true);
});

I know this isn't specifically what you asked but might be the easiest way.

2 Comments

I don't see what this accomplishes. Users don't edit form input fields after they hit the submit button anyway.
@Asaph the disabled attribute will cause it to not post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.