user2867494
user2867494

Reputation:

How do I change form data on submit?

I am trying to change the case of text entered into a textbox for a search when the form is submitted. I'm having an issue when it comes to submitted the converted text. Using the code below, how can I submit the form with the converted text? I would prefer that the appearance of the text in the text box is not altered.

search.submit(function (e) {
    e.preventDefault();

    convert = searchBox.val();
    converted = convert.toTitleCase();

    this.submit();
});

Upvotes: 0

Views: 328

Answers (3)

Gary Schreiner
Gary Schreiner

Reputation: 902

You are converting the text, but you're not saving it back to the input before you submit.

Try something like this:

search.submit(function (e) {
    e.preventDefault();

    convert = searchBox.val();
    converted = convert.toTitleCase();
    $('#inputID').val(converted);

    this.submit();
});

And I don't believe you need to preventDefault and then submit.. just return true.

EDIT: Per your question in the above answer's comment, try this:

original: <input type=text name="myinput" id="myinput">

new:

html

<input type=text id="myinput">

<input type=hidden name="myinput" id="myinputHidden">

jscript

search.submit(function (e) {
    $('#myinputHidden').val($('#myinput').val().toTitleCase());
});

The name attribute is what the form uses to create it's POST/GET parameters ... if the name attribute doesn't exist, the form doesn't send the value

Upvotes: 2

John
John

Reputation: 1489

Try,

search.submit(function (e) {
  searchBox.val(searchBox.val().toTitleCase());
});

Upvotes: 3

T McKeown
T McKeown

Reputation: 12847

convert = searchBox.val();
converted = convert.toTitleCase();
searchBox.val(converted);

Upvotes: 1

Related Questions