Search code examples
javascripthtmlinserthtml

Insert html using JavaScript without using id?


I want to insert html using JavaScript with something like this : document.getElementById("demo").innerHTML = "example"; but i do not want to use id how can i do it by class name? I have tried document.getElementsByClassName("demo").innerHTML = "example"; but it did not work either...or is there any why to insert html other than innerHTML using classes ..


Solution

  • Try by class document.getElementsByClassName("demo").innerHTML = "TEXT/HTML"

    or

    By tag document.getElementsByTagName("p").innerHTML = "TEXT/HTML"

    You can see this methods here on documentation:

    https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByTagName