Home jquery How to insert HTML markup in JS?

How to insert HTML markup in JS?

Author

Date

Category

Hello.

Tell me how in js insert an HTML block with content?

For example:

& lt; div class = "ds" & gt; text text & lt; / div & gt; & lt; div class = "SDF12" & gt; text text & lt; / div & gt;
& lt; script type = "text / javascript" & gt;
var as = '& lt; div class = "ds" & gt; text text & lt; / div & gt; & lt; div class = "SDF12" & gt; text text & lt; / div & gt;';
& lt; / script & gt;

So it does not work for some reason, but how to connect this file later, and it read the markup?


Answer 1, Authority 100%

http://jsfiddle.net/aqjlk2yk/

http://jquery-docs.ru/attributes/html/

http://jquery-docs.ru/attributes/text/


Answer 2, Authority 50%

You need the value of this variable as somehow insert in the right place of the document. For example, if there is a blank & lt; div & gt; with the known ID , then it is possible (Example 1 ):

document.getelementbyid ("Mesto"). Innerhtml = AS;

or you can, for example, at the end of the document, or the famous element, add freshly directed & lt; div & gt; with your HTML (Example 2 ):

var div = document.createelement ("DIV");
div.innerhtml = as;
Document.getelementsByTagname ('BODY') [0] .Apandchild (DIV);

JQuery makes it easier to the same task. The same two ways will look like one line:

$ ("# Mesto"). HTML (AS); // in an element with id = "Mesto"
// or
$ ("Body"). Append (AS); // at the end of the document, without turning in addition. Div

If HTML is a lot of , in a few lines, you can wrap it in a separate tag with ID:

& lt; script id = "html-1" type = "text / template" & gt; // Note that it is not javascript!
  & lt; div class = "ds" & gt; text text & lt; / div & gt;
  & lt; div class = "SDF12" & gt; text text & lt; / div & gt;
& lt; / script & gt;
& lt; script & gt; // And this is normal JavaScript
  VAR AS = Document.getelementByid ("HTML-1"). Innerhtml;
  // or the same with jQuery:
  var as = $ ("html-1"). HTML ();
  // and then as always :)
  // ...
& lt; / script & gt;

Answer 3

& lt; div id = "ds" & gt; text text & lt; / div & gt; & lt; div id = "SDF12" & gt; text text & lt; / div & gt;
& lt; script type = "text / javascript" & gt;
  Document.getelementByid ("DS"). Innerhtml = "text text";
  Document.getelementByid ("SDF12"). Innerhtml = "Text Text";
& lt; / script & gt;
Previous articleData Comparison
Next articleWhat is 1 px

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions