rammanoj
rammanoj

Reputation: 497

Create html elements instead of selecting them jquery

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

Upvotes: 0

Views: 48

Answers (1)

Thaw
Thaw

Reputation: 861

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

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

console.log(html.prop('outerHTML'));

Upvotes: 1

Related Questions