1

I need to append a html file inside a div.

While doing jquery html(), it is removing the html & body tags.

How to retain those tags?

Refer this snippet:

$('.html-file').html('<html><p>Hello</p></html>'); //it is removing html tag
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="html-file">
</div>

5
  • There is no need to add the <html> tag agian. This should only be added once Commented Nov 9, 2017 at 7:08
  • if you want to show the text then .text() is what you need Commented Nov 9, 2017 at 7:09
  • 1
    The question is, why do you want to keep them? What's the real problem you're trying to solve? Commented Nov 9, 2017 at 7:14
  • I am getting a html mail template file from server and put it in a div so that the user can edit the text & I will update those changes in html directly & finally I will send that html to the server. Commented Nov 9, 2017 at 7:18
  • 1
    Yeah, well, this is really not the right way to do it. Interactive HTML editor inside a web page is a bit more complicated than that. Commented Nov 9, 2017 at 7:22

2 Answers 2

1
$('.html-file').html('<p>Hello</p>'); 

There is no need to add html tag in div

Sign up to request clarification or add additional context in comments.

Comments

0

Try:

$('.html-file').append('<p>Hello</p>')

If you need to 'append' content to div.

Comments

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.