0

I am trying to create an HTML document using jquery. This is the following code I want to create

<html charset=\'utf-8\'>
   <head>
   </head>
   <body>
      <table>
         <tbody>
            <tr>
               <td> sample text -1</td>
            </tr>
            <tr>
               <td> sample text -2</td>
            </tr>
         </tbody>
      </table>
   </body>
</html>

I want to create this and append it to a file. I don't want to create this using string appending. But instead it is better creating jquery elements. Tried the following:

var obj = $( 'html' );
console.log(obj.html());

Executing above code is printing the content inside html tag of present html file instead of creating a new html tag. So, is there any way to create an object of above code and stringify it? Thanks in advance

Aneesh R S
  • 3,807
  • 4
  • 23
  • 35
rammanoj
  • 479
  • 8
  • 26
  • https://stackoverflow.com/questions/327047/what-is-the-most-efficient-way-to-create-html-elements-using-jquery – Alpesh Jikadra Apr 05 '18 at 04:11
  • You can only have one `` tag on the page at any given time. Though if you want to insert elements to the page, simply use `insertNode()`. – Obsidian Age Apr 05 '18 at 04:17
  • Instead of string appending though, why not just use a string template? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals String templates can go multiline without appending, and you can inject varaibles into them with ${variable} – Taplar Apr 05 '18 at 04:17

1 Answers1

1
var html = $("<html>")
var head = $("<head>");
var body = $("<body>");

html.append(head);
html.append(body);

console.log(html.prop('outerHTML'));
Abdul Ahad
  • 826
  • 8
  • 16