views:

679

answers:

2

I'm collecting information in a standard HTML form. I have, for example, <input type="text" name="UserName" id="name"/>. When the form is submitted, I want to add a line break to the value entered. So if the user entered "foo," the form would submit the value "foo\n."

Here's the jQuery function I'm using:

$("#formID").submit(function () {
    $(":text").each(function () {
        var value = $(this).val();
        var newValue = value + " \n";
        $(this).val(newValue);
    });
});

When the form is submitted, however, the value assigned to the form field does not have the line break.

My goal is for the line break to survive the output of the back-end form processing, which generates an email. Since I don't have control over the script that generates the mail, I'm trying to impose some formatting with what I can control.

Any assistance is appreciated.

+1  A: 

Textbox doesn't holds "\n" only textarea does. What you can do is post the data yourself to the controller or store the values in hidden fields and submit the form and read the hidden fields in the server, if you don't have much control there match the names to the hidden fields.

Teja Kantamneni
So you're recommending that I take the value of text field "foo" and assign it to a hidden field "bar," append the \n to the value of "bar" and then submit the form? I'll give that a shot.
theJBRU
A: 

I posted my previous answer before I saw your comment that the output is plain text. Now try this:

$(document).ready(function() {  
    $("#formID").submit(function () {
        $(":text").each(function () {
            var value = $(this).val();
            var myname  = $(this).attr('name');
            var newValue = value + " \n";
            var hid   = '<input type="hidden" name="' + myname + '" value="' + newValue + '"/>';
            $(this).removeAttr('name');
            $("#formID").append(hid);
        });
    });
});

Previous Answer

As Mark says, the line break character is not rendered as a line break when viewed in the browser (or email client for that matter), so instead of adding a line break character "\n", you should add a <br/>

$("#formID").submit(function () {
    $(":text").each(function () {
        var value = $(this).val();
        var newValue = value + '<br/>';
        $(this).val(newValue);
    });
})
Majid
This, unfortunately, simply adds the text "<br/>" to the field value. The HTML line break isn't rendered in the mail. Appreciate the effort!
theJBRU
The second approach is based on the hint given by Teja Kantamneni, but instead of assigning the value of foo textbox to bar hidden input I am simply using the same name for the hidden input, this way the backend script will get the name-value pair.
Majid
I like this approach, @Majid. My first attempt didn't work out, as it appears that what the script received was blank values. For example, the script's error checking thought the email input was invalid as it was blank. So I think the first thing I'll do is swap the ":text" selector for a class I can apply to those fields I want this manipulation done to. Thanks again.
theJBRU
There was a mistake in the code I posted. It was last line which should be $("#formID").append(hid); NOT $("#myform").append(hid); I just noticed this and corrected it.
Majid