-1

I'm developing a website with bootstrap. If I want to modify the navbar, I don't want to go to any html files and make changes. So would like to use javascript to "inject" the html code into the actual html file but I don't know how to do it.

This is what I tried.

document.write("<p>html code here</p>")

It, however, doesn't work. What the most conveniente and simple solution could be?

  • 1
    There are many. Consider researching into any libraries or frameworks? – evolutionxbox Jan 04 '21 at 00:03
  • 2
    don't ever use `document.write` - [it does not do what you think, and it's from the very first days of JS, before we knew better](http://pomax.github.io/1473270609919/if-you-use-use-document-write-you-suck-at-javascript). It's 2021, learn how to use the DOM API (createElement, append, etc). [MDN](https://developer.mozilla.org) is your friend, start with [appendChild](https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild), read the example, and take it from there. – Mike 'Pomax' Kamermans Jan 04 '21 at 00:10
  • 1
    Use a static site generator for this sort of thing. Don't use client-side JS. – Quentin Jan 04 '21 at 22:39

1 Answers1

0

You can try something like this example below:

let newElement = document.createElement('div');

newElement.className = 'new-element';

This will create a div - assign it to the variable named 'newElement' and creating a class associated with that element named "new-element".

Dharman
  • 30,962
  • 25
  • 85
  • 135
Albert Yoo
  • 31
  • 6