Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6681811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:37:57+00:00 2026-05-26T04:37:57+00:00

I have a textarea inside a form : <form action=# id=container> <textarea id=txt cols=100

  • 0

I have a textarea inside a form:

<form action="#" id="container">
  <textarea id="txt" cols="100" rows="5">Good bye</textarea>
</form>

All I want is to replace this form by a div:

$("#container").replaceWith(function() {
    return "<div>" + $(this).html() + "</div>";
});

That works without any problem, but if I before to use the replaceWith method change the textarea value:

$("#txt").val("hello world");

The final content is a textarea inside the div with the “Good bye” text, not the “hello world” as expected. You have a demo here.

Why is this happening? How can I replace the form by a div preserving textarea contents?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:37:57+00:00Added an answer on May 26, 2026 at 4:37 am

    This works:

    $("#txt").val("hello world");
    $("#container").replaceWith(function() {
        return $('<div />').append($(this).contents());
    });
    

    The issue come with serializing the html with .html(). It’s much better to work in terms of DOM Nodes. This’ll also preserve event handlers.


    Here’s another way:

    $('#container').wrapInner('<div/>').children().unwrap();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a textarea block like so... <textarea name=myTextArea cols=100 rows=20 readonly=readonly> ..
I have a textarea inside a form. Before the form is submitted, the textarea
I have a description textarea inside a form where user may enter a description
I have a textarea inside a form. No wrap in the html code is
I have a Textarea inside a TD tag and I want to set the
I need to have a <textarea> take up all available space inside of a
I have this textarea form: self.response.out.write( <form name=title action=/edittitlepitchhandler method=post> ... pitch: <br />
Ok lets say I have div with form elements inside of it. I want
I have a very tricky problem going on with content inside a textarea on
I have textarea in my site(link shortening site) i want to user enter some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.