0

I'm just trying to retrieve the dom of the two html page on the same javascript file.

When i try to retrive the the dom of an element of the first html page, it works, but the elements of the second page return me undefined. How can i try to retrieve the element of the second page ?

firstpage.html

<p id="first">test first</p>

secondpage.html

<p id="second">test second</p>

file.js

var first = document.getElementById("first").innerHTML;
console.log(first); // will show me "test first"
var second = document.getElementById("second").innerHTML;
console.log(second); // will show me "undefined" .. :(

Thanks for the help !

12
  • 1
    can you be a bit discrete about the question, maybe using a code snippet or something? Commented Oct 4, 2018 at 9:38
  • Sure, will edit it right now ;) Commented Oct 4, 2018 at 9:40
  • 1
    You're trying to approach a solution in wrong direction. Things doesn't have to be this complicated. I don't know what exactly you're trying to achieve. Maybe you can take a look at SPA? Commented Oct 4, 2018 at 10:19
  • Check your console for any possible error. Commented Oct 4, 2018 at 10:32
  • got the problem, JAVASCRIPT is an interpreted language, hence in the first case it gets the id 'first' and console's the result, but look carefully it also gets the error for the second one saying Cannot read property 'innerHTML' of null. That is because in the first page it doesn't get's the id = 'second'. Commented Oct 4, 2018 at 10:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.