Home > Back-end >  How to get element by id for 2 documents
How to get element by id for 2 documents

Time:07-26

I have a website that at file1.html the user presses a button and it changes the code at file2.html, but it is not working. Html file1: <button type="button onclick="functionname()"> <script src="scriptname.js">

Html file 2: <p id="idname"> <script src="scriptname.js"

Javascript:function functionname(){ document.getElementById("idname").innerHTML="Some text" }

CodePudding user response:

You will need a variable that can be carried to the second page. One way is to store the data in a localStorage variable.

<button >Save</button>
<p id="idname"></p>

    let btn = document.querySelector(".btn");
    let idname = document.querySelector("#idname");
    
    btn.addEventListener("click",function(){
        localStorage.setItem("varname","Some Text")
    });
    
    if(idname){
    idname.innerHTML = localStorage.getItem("varname") || ""
    }
  • Related