-1

I have 2 html files connected to one js file. When I try to access a html element in the second html file using js it doesn't work saying that is is null. I did let elementname = document.getElementById("element") for a element in the second html page then console.log(elementname) and it says it is null. When I do it for a element in the first html page it says HTMLButtonElement {}

Here is the html for the first Page

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Not Quuuuiiiizzzz</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>

  <body>
    <h1>Not Quuuuiiiizzzz</h1>

    <h2>Join a quiz</h2>

    <!--Buttons -->
    <div style="text-align: center;">
      <button id="btnforquiz1" onclick="gotoquiz()"></button>
      <button id="btnforquiz2" onclick="gotoquiz1()"></button>
      <button id="btnforquiz3" onclick="gotoquiz2()"></button> 
    </div>
    <h2 id="h2">Create a Quuuuiiiizzzz</h2>

    <script src="script.js"></script>
  </body>
</html>

For the second page

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Not Quuuuiiiizzzz</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>

  <body onload="quizLoad()">

    <h1 id="question">Hello</h1>

    <button id="answer1"></button>
    <button id="answer2"></button>
    <button id="answer3"></button>
    <button id="answer4"></button>

    <script src="script.js"></script>
  </body>
</html>

And Finally for the js file :

//setting global variables
let btn1 = document.getElementById("btnforquiz1") //getting button with id of btnforquiz1 repeat below
correct = 0
let btn2 = document.getElementById("btnforquiz2") 
let btn3 = document.getElementById("btnforquiz3") 
let question = document.getElementById("question")
let answer1 = document.getElementById("answer1")
let answer2 = document.getElementById("answer2")
let answer3 = document.getElementById("answer3")
let answer4 = document.getElementById("answer4")
quizNameRel = -1;
cosnole.log(question)
console.log(answer1)

//Quiz Data 
Quiz_1 = {
  "What is the capital of buffalo":["Idk", "Yes", "No",0],
  "What is the smell of poop": ["Stinky"]
};
Quiz_2 = [
  "What is wrong with you"
];
Quiz_3 = [
  "What is wrong with you #2"
]
let quiz = { 
  name: ["History Test", "Math Practice", "ELA Practice"],
  mappingtoans: [0,1,2],
  QA: [Quiz_1, Quiz_2, Quiz_3]
}
//quiz data




//when body loades run showQuizzs function
document.body.onload = showQuizzs()

function showQuizzs() {
  //loops throo the vals seeting the text for the btns
  for (let i = 0; i < quiz.name.length; i++) {
    btn1.textContent = quiz.name[i-2]
    btn2.textContent = quiz.name[i-1]
    btn3.textContent = quiz.name[i]
  }
}

//leads to the showQuizzs
function gotoquiz() {
  location.href = "quiz.html"
  quizNameRel = quiz.name[0]//I was trying to create a relation so we could knoe which quiz they wnt to do 
  startQuiz()
}

function gotoquiz1() {
  location.href = "quiz.html"
  quizNameRel = quiz.name[1]
  startQuiz()
}

function gotoquiz2() {
  location.href = "quiz.html";
  quizNameRel = quiz.name[2];
  startQuiz();
}
function answerselect(elements){
  whichone = Number(elements.id.slice(-2,-1))
  if(Quiz_1[whichone]==Quiz_1[-1]){
    correct+=1;
    NextQuestion();
  }else{
    wrong+=1;
  }
}
//gets the keys and puts it into an array
function getkeys(dictionary){
  tempdict = [];
  for(i in dictionary){
    tempdict.push(i);
  }
  return tempdict;
}

function setQuestion() {
  let tempdict = getkeys(Quiz_1)
  console.log(tempdict, getkeys(Quiz_1));
  //question.innerHTML = tempdict;
}

// startQuiz
function startQuiz() {
  switch (quizNameRel){
    case quiz.name[0]:
    //case here
      setQuestion()
      break
    case quiz.name[1]:
    //case here
      break
    case quiz.name[2]:
    //case here
      break
  }
}

//TO DO:
//  Set the question
//  Set the answer
//  Check if correct button 
Sup1213
  • 3
  • 2
  • When you set the `location.href` you’re performing a redirection and the next page will not have access to whatever data the previous page has set. Use local or session storage instead. It seems like you’re lacking a fundamental understanding of what runtime is. – Terry Sep 18 '21 at 15:34
  • _`//when body loades run showQuizzs function` `document.body.onload = showQuizzs()`_ — No, when the body loads, nothing will happen. `showQuizzs()` is not a function; `showQuizzs` is. See [In JavaScript, does it make a difference if I call a function with parentheses?](/q/3246928/4642212). – Sebastian Simon Sep 18 '21 at 15:36

1 Answers1

0

This is happening because at a time you have rendered only one html file. For example if you render index1.html(first file) then your js will look for rendered element from first file only but here index2.html(second file) is not rendered so your js script is unable to find elements of that file that's the reason it shows null.

If you try to render now index2.html rather than index1.html then you will find now elements from index2.html are detected by js script but elements from index1.html are null now.

Gulshan Aggarwal
  • 954
  • 1
  • 7
  • 13