Maybe a bug in JavaScript?

Joined
Sep 2, 2022
Messages
2
Reaction score
0
Alright so when I add a id tag to a html element [ <div id = 'test'>hi<div> ] and use [ document.querySelector("#test").style.backgroundColor = "green"; ], it works.
However the second I use [ mainwindow.id = 'test'; ] in [ const mainWindow = document.createElement("div"); document.body.appendChild(mainWindow); mainwindow.id = 'test'; ] it doesn't work! Why is this happening? if you find it out please let me know.

(Sorry for the messy code I'm making a bookmarklet application)
 
Last edited:
Joined
Sep 4, 2022
Messages
128
Reaction score
16
JavaScript:
<html>
<head>
  <script language="javascript">

  function create(){

   var mainWindow = document.createElement("div"); // global VAR
   mainWindow.id = 'success';

   _add = document.querySelector('#test'); // local VAR
   
   _add.appendChild(mainWindow);

  mainWindow; // = Null
  _add; // = Null, reset of the var

}

</script>

</head>

<body onload="create();">

<div id="test"></div>

</body></html>

prefer using 'let' instead of 'const' , because 'const' are fixed.
 
Joined
Sep 4, 2022
Messages
128
Reaction score
16
JavaScript:
<html>
<head>
  <script language="javascript">

  function create(){

   var mainWindow = document.createElement("div"); // global VAR
   mainWindow.id = 'success';

   document.querySelector('#test').appendChild(mainWindow);

}

</script>

by js syntax , you can make an imperative instruction :
document.querySelector('#test').appendChild(mainWindow);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top