scojomodena
scojomodena

Reputation: 842

Adding a newline in a textarea using Javascript

I have an iframe popup that allows the user to add information. After it's added, I want it to be added to some hidden inputs (textareas in this case).

I need to add a concatenated address string using the following C# code:

public string AddressInputFormatted { get { return string.Format("{0}{4}{1}, {2} {3}", Address, City, State, Zip, Environment.NewLine); } }

or even (tried both):

public string AddressInputFormatted { get { return string.Format("{0}\n{1}, {2} {3}", Address, City, State, Zip); } }

I get that and create a startup script on postback:

    Dim strScript As String = "window.parent.$('.contact.name').html('{0}');" & _
                    "window.parent.$('.contact.title').html('{1}');" & _
                    "window.parent.$('.contact.email').html('<a href=mailto:{2}>{2}</a>');" & _
                    "window.parent.$('.contact.address').html('{3}');" & _
                    "window.parent.$('textarea.contact.address').val('{9}');" & _
                    "window.parent.$('.contact.work-phone').html('Phone: {4}');" & _
                    "window.parent.$('.contact.mobile-phone').html('Mobile: {5}');" & _
                    "window.parent.$('.contact.fax').html('Fax: {6}');" & _
                    "window.parent.$('#hiddenCustomerID').val({7});" & _
                    "window.parent.$('.getCustomerName').val('{8}'); window.parent.$('.customer.name').html('{8}'); parent.$.fn.colorbox.close();" & _
                    "window.parent.$('.change-contact').show(); window.parent.$('.add-contact, .autoCompleteCustomerContacts').hide();" & _
                    "window.parent.$('.change-customer').show(); window.parent.$('.add-customer, .autoCompleteCustomersWithID').hide();"

strScript = String.Format(strScript, contact.FullName, contact.Title, contact.Email, contact.AddressInputFormatted, contact.WorkPhone, contact.MobilePhone, contact.Fax, contact.CustomerID, customer.Name, contact.Address)
ScriptManager.RegisterStartupScript(Page, Me.GetType(), "updateParent", " $(document).ready(function () {" & strScript & " });", True)

Either way, I see the actually javascript with a line return on the page, and I get a javascript error:

SyntaxError: Unexpected token ILLEGAL

What am I doing wrong? How can I store a new line in the hidden text area using javascript?

Upvotes: 0

Views: 105

Answers (1)

Shadow Wizard
Shadow Wizard

Reputation: 66399

You need to escape the escape character so that the HTML output will contain \n:

public string AddressInputFormatted
{
    get
    {
        return string.Format("{0}\\n{1}, {2} {3}", Address, City, State, Zip);
    }
}

Upvotes: 1

Related Questions