How to fix “Cannot read property 'getElementsByClassName' of null” error in JavaScript [duplicate]Why does jQuery or a DOM method such as getElementById not find the element?How to validate an email address in JavaScript?How do JavaScript closures work?How to check empty/undefined/null string in JavaScript?How to change an element's class with JavaScript?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I include a JavaScript file in another JavaScript file?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

What are these round pads on the bottom of a PCB?

Does a surprised creature obey the 1st level spell Command?

How to avoid making self and former employee look bad when reporting on fixing former employee's work?

What is the minimum required technology to reanimate someone who has been cryogenically frozen?

TeX Gyre Pagella Math Integral sign much too small

Can I bring back Planetary Romance as a genre?

When do you stop "pushing" a book?

What is the Ancient One's mistake?

My perfect evil overlord plan... or is it?

Why did they wait for Quill to arrive?

Passport stamps art, can it be done?

Is there an application which does HTTP PUT?

Best species to breed to intelligence

Has everyone forgotten about wildfire?

Employee is self-centered and affects the team negatively

Is it safe to keep the GPU on 100% utilization for a very long time?

Program for finding longest run of zeros from a list of 100 random integers which are either 0 or 1

Thawing Glaciers return to hand interaction

Names of the Six Tastes

Can the president of the United States be guilty of insider trading?

Probability of taking balls without replacement from a bag question

How does weapons training transfer to empty hand?

What is the radius of the circle in this problem?

if i accidentally leaked my schools ip address and someone d doses my school am i at fault



How to fix “Cannot read property 'getElementsByClassName' of null” error in JavaScript [duplicate]


Why does jQuery or a DOM method such as getElementById not find the element?How to validate an email address in JavaScript?How do JavaScript closures work?How to check empty/undefined/null string in JavaScript?How to change an element's class with JavaScript?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I include a JavaScript file in another JavaScript file?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1
















This question already has an answer here:



  • Why does jQuery or a DOM method such as getElementById not find the element?

    6 answers



I am trying to learn JavaScript. I have a test file, but it gives me an error.
The result should be 3 empty divs.



Error: Cannot read property 'getElementsByClassName' of null


I've tried debugging and changing item=template.getElementsByClassName("main"); to item=template.querySelectorAll("#template div");



HTML



<body>
<script src="test.js" charset="utf-8"></script>
<template id="test">
<div class="main">
<div class="">

</div>
</div>
</template>
<div class="site">

</div>
</body>


JavaScript



var template, item, site, i, a;

template=document.getElementById("test");
item=template.getElementsByClassName("main");

site=document.getElementsByClassName("site");
for(i=0; i<3; i++)
a=document.importNode(item, true);
site.appendChild(a);











share|improve this question















marked as duplicate by Quentin html
Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 23 at 9:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























    -1
















    This question already has an answer here:



    • Why does jQuery or a DOM method such as getElementById not find the element?

      6 answers



    I am trying to learn JavaScript. I have a test file, but it gives me an error.
    The result should be 3 empty divs.



    Error: Cannot read property 'getElementsByClassName' of null


    I've tried debugging and changing item=template.getElementsByClassName("main"); to item=template.querySelectorAll("#template div");



    HTML



    <body>
    <script src="test.js" charset="utf-8"></script>
    <template id="test">
    <div class="main">
    <div class="">

    </div>
    </div>
    </template>
    <div class="site">

    </div>
    </body>


    JavaScript



    var template, item, site, i, a;

    template=document.getElementById("test");
    item=template.getElementsByClassName("main");

    site=document.getElementsByClassName("site");
    for(i=0; i<3; i++)
    a=document.importNode(item, true);
    site.appendChild(a);











    share|improve this question















    marked as duplicate by Quentin html
    Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    Mar 23 at 9:10


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















      -1












      -1








      -1









      This question already has an answer here:



      • Why does jQuery or a DOM method such as getElementById not find the element?

        6 answers



      I am trying to learn JavaScript. I have a test file, but it gives me an error.
      The result should be 3 empty divs.



      Error: Cannot read property 'getElementsByClassName' of null


      I've tried debugging and changing item=template.getElementsByClassName("main"); to item=template.querySelectorAll("#template div");



      HTML



      <body>
      <script src="test.js" charset="utf-8"></script>
      <template id="test">
      <div class="main">
      <div class="">

      </div>
      </div>
      </template>
      <div class="site">

      </div>
      </body>


      JavaScript



      var template, item, site, i, a;

      template=document.getElementById("test");
      item=template.getElementsByClassName("main");

      site=document.getElementsByClassName("site");
      for(i=0; i<3; i++)
      a=document.importNode(item, true);
      site.appendChild(a);











      share|improve this question

















      This question already has an answer here:



      • Why does jQuery or a DOM method such as getElementById not find the element?

        6 answers



      I am trying to learn JavaScript. I have a test file, but it gives me an error.
      The result should be 3 empty divs.



      Error: Cannot read property 'getElementsByClassName' of null


      I've tried debugging and changing item=template.getElementsByClassName("main"); to item=template.querySelectorAll("#template div");



      HTML



      <body>
      <script src="test.js" charset="utf-8"></script>
      <template id="test">
      <div class="main">
      <div class="">

      </div>
      </div>
      </template>
      <div class="site">

      </div>
      </body>


      JavaScript



      var template, item, site, i, a;

      template=document.getElementById("test");
      item=template.getElementsByClassName("main");

      site=document.getElementsByClassName("site");
      for(i=0; i<3; i++)
      a=document.importNode(item, true);
      site.appendChild(a);






      This question already has an answer here:



      • Why does jQuery or a DOM method such as getElementById not find the element?

        6 answers







      javascript html html5 dom






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 23 at 9:22







      Rares

















      asked Mar 23 at 9:09









      RaresRares

      42




      42




      marked as duplicate by Quentin html
      Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Mar 23 at 9:10


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Quentin html
      Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Mar 23 at 9:10


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          1 Answer
          1






          active

          oldest

          votes


















          -1














          The reason it's not working is cause youre trying to fetch 3 elements who's class name is "main". But only 1 exists that's why it complains that it cannot read the property of something that does not exist. So I've updated the code to fetch the 3 empty divs



          Additionally the last div is the site div, and you're trying to add elements to it. You'll run into a problem where you will try to add itself to itself. I've updated the code slightly so you don't run into this problem



          var template, item, site, i, a;

          template=document.getElementById("test");
          item=template.querySelectorAll("#template div");

          site=document.getElementsByClassName("site")[0];
          for(i=0; i<item.length; i++)
          a=document.importNode(item, true);
          site.appendChild(a);






          share|improve this answer





























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            -1














            The reason it's not working is cause youre trying to fetch 3 elements who's class name is "main". But only 1 exists that's why it complains that it cannot read the property of something that does not exist. So I've updated the code to fetch the 3 empty divs



            Additionally the last div is the site div, and you're trying to add elements to it. You'll run into a problem where you will try to add itself to itself. I've updated the code slightly so you don't run into this problem



            var template, item, site, i, a;

            template=document.getElementById("test");
            item=template.querySelectorAll("#template div");

            site=document.getElementsByClassName("site")[0];
            for(i=0; i<item.length; i++)
            a=document.importNode(item, true);
            site.appendChild(a);






            share|improve this answer



























              -1














              The reason it's not working is cause youre trying to fetch 3 elements who's class name is "main". But only 1 exists that's why it complains that it cannot read the property of something that does not exist. So I've updated the code to fetch the 3 empty divs



              Additionally the last div is the site div, and you're trying to add elements to it. You'll run into a problem where you will try to add itself to itself. I've updated the code slightly so you don't run into this problem



              var template, item, site, i, a;

              template=document.getElementById("test");
              item=template.querySelectorAll("#template div");

              site=document.getElementsByClassName("site")[0];
              for(i=0; i<item.length; i++)
              a=document.importNode(item, true);
              site.appendChild(a);






              share|improve this answer

























                -1












                -1








                -1







                The reason it's not working is cause youre trying to fetch 3 elements who's class name is "main". But only 1 exists that's why it complains that it cannot read the property of something that does not exist. So I've updated the code to fetch the 3 empty divs



                Additionally the last div is the site div, and you're trying to add elements to it. You'll run into a problem where you will try to add itself to itself. I've updated the code slightly so you don't run into this problem



                var template, item, site, i, a;

                template=document.getElementById("test");
                item=template.querySelectorAll("#template div");

                site=document.getElementsByClassName("site")[0];
                for(i=0; i<item.length; i++)
                a=document.importNode(item, true);
                site.appendChild(a);






                share|improve this answer













                The reason it's not working is cause youre trying to fetch 3 elements who's class name is "main". But only 1 exists that's why it complains that it cannot read the property of something that does not exist. So I've updated the code to fetch the 3 empty divs



                Additionally the last div is the site div, and you're trying to add elements to it. You'll run into a problem where you will try to add itself to itself. I've updated the code slightly so you don't run into this problem



                var template, item, site, i, a;

                template=document.getElementById("test");
                item=template.querySelectorAll("#template div");

                site=document.getElementsByClassName("site")[0];
                for(i=0; i<item.length; i++)
                a=document.importNode(item, true);
                site.appendChild(a);







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 23 at 9:17









                Craig WayneCraig Wayne

                2,39732235




                2,39732235















                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript