NodeJS - How to insert an element in a HTML document using Javascript?

F.J. Klaiber Aboitiz

I'm trying to use NodeJS to modify an external HTML file (which is located in the same directory). In my index.js file I write:

fs.readFile('index.html', (err,html)=>{
  if(err){
     throw err;
  }

html.body.innerHTML += '<div id = "asdf"></div>';

});

As index.html is a valid document. But it doesn't look to be reading it properly, as I get as an error:

"TypeError: Cannot read property 'innerHTML' of undefined".

I guess that html is not getting anything as body.

How can I do changes in HTML using JavaScript?

Jeanluca Scaljeri

Here is an example using node-html-parse

HTML file

<html>
   <body>
      <div id="fist">yolo</div>
   </body>
</html>

And the nodejs

const fs = require('fs');
const parse = require('node-html-parser').parse;

fs.readFile('index.html', 'utf8', (err,html)=>{
   if(err){
      throw err;
   }

   const root = parse(html);

   const body = root.querySelector('body');
   //body.set_content('<div id = "asdf"></div>');
   body.appendChild('<div id = "asdf"></div>');

   console.log(root.toString()); // This you can write back to file!
 });

There might be better solutions than node-html-parser, considering the amount of downloads. For example, htmlparser2 has much more downloads, but it also looks more complex :)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to join array of objects in javascript and then insert into html element

分類Dev

Insert element in html using java script

分類Dev

How to insert NULL value from nodeJS/javascript to MySQL using parameterized inputs?

分類Dev

how can i lowercase object value/data in javascript using underscore for HTML5 document

分類Dev

How to print an HTML document using Puppeteer?

分類Dev

How to update html document using feather js?

分類Dev

How to insert Nested document in solr using spring data solr?

分類Dev

How to insert Nested document in solr using spring data solr?

分類Dev

How to insert Nested document in solr using spring data solr?

分類Dev

How to serve multiple html files using NodeJS

分類Dev

How to insert markup into a table using javascript

分類Dev

How to find an element by data selector in an array of HTML DOM objects using JavaScript?

分類Dev

How to hide child element using javascript

分類Dev

How to properly copy/clone DOM element and then paste/insert it as HTML element in Angular.js?

分類Dev

How to insert a PHP code which has HTML tag inside the Javascript

分類Dev

Is it possible to insert an HTML tag from pseudo-element content using CSS?

分類Dev

How to pass LatlngBounds object to nodejs server using javascript

分類Dev

javascript - How to assign json data to an HTML element and send to API onclick?

分類Dev

How to conditionally render CSS and JavaScript inside head element of an HTML

分類Dev

How to create an element in JavaScript, under a specific HTML tag

分類Dev

How to call a javascript from html using an id

分類Dev

How to display html file in iframe using javascript

分類Dev

Extract <svg> element by using document.evaluate()?

分類Dev

How can I insert a whole css class before an HTML element that I find?

分類Dev

How to find an element in nested child array document in MongoDB using c#

分類Dev

Using typescript, how to update HTML element's value and text.?

分類Dev

How to hide HTML element containing only spaces using jQuery?

分類Dev

How to target second child element of a div using JAVASCRIPT

分類Dev

How to dynamically check if an element has mulitple class using jquery or javascript

Related 関連記事

  1. 1

    How to join array of objects in javascript and then insert into html element

  2. 2

    Insert element in html using java script

  3. 3

    How to insert NULL value from nodeJS/javascript to MySQL using parameterized inputs?

  4. 4

    how can i lowercase object value/data in javascript using underscore for HTML5 document

  5. 5

    How to print an HTML document using Puppeteer?

  6. 6

    How to update html document using feather js?

  7. 7

    How to insert Nested document in solr using spring data solr?

  8. 8

    How to insert Nested document in solr using spring data solr?

  9. 9

    How to insert Nested document in solr using spring data solr?

  10. 10

    How to serve multiple html files using NodeJS

  11. 11

    How to insert markup into a table using javascript

  12. 12

    How to find an element by data selector in an array of HTML DOM objects using JavaScript?

  13. 13

    How to hide child element using javascript

  14. 14

    How to properly copy/clone DOM element and then paste/insert it as HTML element in Angular.js?

  15. 15

    How to insert a PHP code which has HTML tag inside the Javascript

  16. 16

    Is it possible to insert an HTML tag from pseudo-element content using CSS?

  17. 17

    How to pass LatlngBounds object to nodejs server using javascript

  18. 18

    javascript - How to assign json data to an HTML element and send to API onclick?

  19. 19

    How to conditionally render CSS and JavaScript inside head element of an HTML

  20. 20

    How to create an element in JavaScript, under a specific HTML tag

  21. 21

    How to call a javascript from html using an id

  22. 22

    How to display html file in iframe using javascript

  23. 23

    Extract <svg> element by using document.evaluate()?

  24. 24

    How can I insert a whole css class before an HTML element that I find?

  25. 25

    How to find an element in nested child array document in MongoDB using c#

  26. 26

    Using typescript, how to update HTML element's value and text.?

  27. 27

    How to hide HTML element containing only spaces using jQuery?

  28. 28

    How to target second child element of a div using JAVASCRIPT

  29. 29

    How to dynamically check if an element has mulitple class using jquery or javascript

ホットタグ

アーカイブ