Access an element in a newly opened window












1















I want to open an html file with JavaScript and write some data into some of its elements. I tried the following:



var info_window = window.open('info_print.html')
info_window.document.getElementById('new_info').innerHTML = data;


But I get this error while I have the #new_info element:



cannot set property 'innerHTML' of null


what should I do?
I checked the commented question but it was not as my question.










share|improve this question




















  • 1





    Looks like there's no element with id equal to new_info in the new window. You're probably either searching for the wrong element or (more likely) not waiting enough time for the window to load the element you want.

    – Marco Bonelli
    Nov 15 '18 at 12:22






  • 7





    Possible duplicate of Add content to a new open window

    – richflow
    Nov 15 '18 at 12:22











  • @richflow Not really a duplicate of that! The other question is asking for a totally different thing, and the suggested answers don't take into consideration the loading time of the window, since the other question's OP didn't need to. This is a totally different situation!

    – Marco Bonelli
    Nov 15 '18 at 12:31













  • @MarcoBonelli my bad! please let me know what I should do to correct this.

    – richflow
    Nov 17 '18 at 6:53











  • @richflow well you can retract the close vote if you want, just click on "close" again and a menu will pop up

    – Marco Bonelli
    Nov 17 '18 at 10:47
















1















I want to open an html file with JavaScript and write some data into some of its elements. I tried the following:



var info_window = window.open('info_print.html')
info_window.document.getElementById('new_info').innerHTML = data;


But I get this error while I have the #new_info element:



cannot set property 'innerHTML' of null


what should I do?
I checked the commented question but it was not as my question.










share|improve this question




















  • 1





    Looks like there's no element with id equal to new_info in the new window. You're probably either searching for the wrong element or (more likely) not waiting enough time for the window to load the element you want.

    – Marco Bonelli
    Nov 15 '18 at 12:22






  • 7





    Possible duplicate of Add content to a new open window

    – richflow
    Nov 15 '18 at 12:22











  • @richflow Not really a duplicate of that! The other question is asking for a totally different thing, and the suggested answers don't take into consideration the loading time of the window, since the other question's OP didn't need to. This is a totally different situation!

    – Marco Bonelli
    Nov 15 '18 at 12:31













  • @MarcoBonelli my bad! please let me know what I should do to correct this.

    – richflow
    Nov 17 '18 at 6:53











  • @richflow well you can retract the close vote if you want, just click on "close" again and a menu will pop up

    – Marco Bonelli
    Nov 17 '18 at 10:47














1












1








1








I want to open an html file with JavaScript and write some data into some of its elements. I tried the following:



var info_window = window.open('info_print.html')
info_window.document.getElementById('new_info').innerHTML = data;


But I get this error while I have the #new_info element:



cannot set property 'innerHTML' of null


what should I do?
I checked the commented question but it was not as my question.










share|improve this question
















I want to open an html file with JavaScript and write some data into some of its elements. I tried the following:



var info_window = window.open('info_print.html')
info_window.document.getElementById('new_info').innerHTML = data;


But I get this error while I have the #new_info element:



cannot set property 'innerHTML' of null


what should I do?
I checked the commented question but it was not as my question.







javascript jquery html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 8:49







Nawras

















asked Nov 15 '18 at 12:20









NawrasNawras

437




437








  • 1





    Looks like there's no element with id equal to new_info in the new window. You're probably either searching for the wrong element or (more likely) not waiting enough time for the window to load the element you want.

    – Marco Bonelli
    Nov 15 '18 at 12:22






  • 7





    Possible duplicate of Add content to a new open window

    – richflow
    Nov 15 '18 at 12:22











  • @richflow Not really a duplicate of that! The other question is asking for a totally different thing, and the suggested answers don't take into consideration the loading time of the window, since the other question's OP didn't need to. This is a totally different situation!

    – Marco Bonelli
    Nov 15 '18 at 12:31













  • @MarcoBonelli my bad! please let me know what I should do to correct this.

    – richflow
    Nov 17 '18 at 6:53











  • @richflow well you can retract the close vote if you want, just click on "close" again and a menu will pop up

    – Marco Bonelli
    Nov 17 '18 at 10:47














  • 1





    Looks like there's no element with id equal to new_info in the new window. You're probably either searching for the wrong element or (more likely) not waiting enough time for the window to load the element you want.

    – Marco Bonelli
    Nov 15 '18 at 12:22






  • 7





    Possible duplicate of Add content to a new open window

    – richflow
    Nov 15 '18 at 12:22











  • @richflow Not really a duplicate of that! The other question is asking for a totally different thing, and the suggested answers don't take into consideration the loading time of the window, since the other question's OP didn't need to. This is a totally different situation!

    – Marco Bonelli
    Nov 15 '18 at 12:31













  • @MarcoBonelli my bad! please let me know what I should do to correct this.

    – richflow
    Nov 17 '18 at 6:53











  • @richflow well you can retract the close vote if you want, just click on "close" again and a menu will pop up

    – Marco Bonelli
    Nov 17 '18 at 10:47








1




1





Looks like there's no element with id equal to new_info in the new window. You're probably either searching for the wrong element or (more likely) not waiting enough time for the window to load the element you want.

– Marco Bonelli
Nov 15 '18 at 12:22





Looks like there's no element with id equal to new_info in the new window. You're probably either searching for the wrong element or (more likely) not waiting enough time for the window to load the element you want.

– Marco Bonelli
Nov 15 '18 at 12:22




7




7





Possible duplicate of Add content to a new open window

– richflow
Nov 15 '18 at 12:22





Possible duplicate of Add content to a new open window

– richflow
Nov 15 '18 at 12:22













@richflow Not really a duplicate of that! The other question is asking for a totally different thing, and the suggested answers don't take into consideration the loading time of the window, since the other question's OP didn't need to. This is a totally different situation!

– Marco Bonelli
Nov 15 '18 at 12:31







@richflow Not really a duplicate of that! The other question is asking for a totally different thing, and the suggested answers don't take into consideration the loading time of the window, since the other question's OP didn't need to. This is a totally different situation!

– Marco Bonelli
Nov 15 '18 at 12:31















@MarcoBonelli my bad! please let me know what I should do to correct this.

– richflow
Nov 17 '18 at 6:53





@MarcoBonelli my bad! please let me know what I should do to correct this.

– richflow
Nov 17 '18 at 6:53













@richflow well you can retract the close vote if you want, just click on "close" again and a menu will pop up

– Marco Bonelli
Nov 17 '18 at 10:47





@richflow well you can retract the close vote if you want, just click on "close" again and a menu will pop up

– Marco Bonelli
Nov 17 '18 at 10:47












2 Answers
2






active

oldest

votes


















1














Assuming your info_print.html actually contains the element you're looking for, then you need to wait for the Window to load it before accessing it.



Here's a simple solution using the onload event:



var info_window = window.open('info_print.html');

info_window.addEventListener('load', function() {
info_window.document.getElementById('new_info').innerHTML = data;
});





share|improve this answer































    1














    I have created one fiddle where it is working fine.
    can you please check if your html file contains new_info element or not or if you are getting any errors in console.



    below is my fiddle code.



    var w = window.open('', "", "width=600, height=400, scrollbars=yes");
    //assuming html as your info_print.html file which contains new_info element
    var html = '<div id="new_info"></div>';
    w.document.body.innerHTML = html;
    w.document.getElementById("new_info").innerHTML="test content";





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319401%2faccess-an-element-in-a-newly-opened-window%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Assuming your info_print.html actually contains the element you're looking for, then you need to wait for the Window to load it before accessing it.



      Here's a simple solution using the onload event:



      var info_window = window.open('info_print.html');

      info_window.addEventListener('load', function() {
      info_window.document.getElementById('new_info').innerHTML = data;
      });





      share|improve this answer




























        1














        Assuming your info_print.html actually contains the element you're looking for, then you need to wait for the Window to load it before accessing it.



        Here's a simple solution using the onload event:



        var info_window = window.open('info_print.html');

        info_window.addEventListener('load', function() {
        info_window.document.getElementById('new_info').innerHTML = data;
        });





        share|improve this answer


























          1












          1








          1







          Assuming your info_print.html actually contains the element you're looking for, then you need to wait for the Window to load it before accessing it.



          Here's a simple solution using the onload event:



          var info_window = window.open('info_print.html');

          info_window.addEventListener('load', function() {
          info_window.document.getElementById('new_info').innerHTML = data;
          });





          share|improve this answer













          Assuming your info_print.html actually contains the element you're looking for, then you need to wait for the Window to load it before accessing it.



          Here's a simple solution using the onload event:



          var info_window = window.open('info_print.html');

          info_window.addEventListener('load', function() {
          info_window.document.getElementById('new_info').innerHTML = data;
          });






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 12:26









          Marco BonelliMarco Bonelli

          23.6k116374




          23.6k116374

























              1














              I have created one fiddle where it is working fine.
              can you please check if your html file contains new_info element or not or if you are getting any errors in console.



              below is my fiddle code.



              var w = window.open('', "", "width=600, height=400, scrollbars=yes");
              //assuming html as your info_print.html file which contains new_info element
              var html = '<div id="new_info"></div>';
              w.document.body.innerHTML = html;
              w.document.getElementById("new_info").innerHTML="test content";





              share|improve this answer




























                1














                I have created one fiddle where it is working fine.
                can you please check if your html file contains new_info element or not or if you are getting any errors in console.



                below is my fiddle code.



                var w = window.open('', "", "width=600, height=400, scrollbars=yes");
                //assuming html as your info_print.html file which contains new_info element
                var html = '<div id="new_info"></div>';
                w.document.body.innerHTML = html;
                w.document.getElementById("new_info").innerHTML="test content";





                share|improve this answer


























                  1












                  1








                  1







                  I have created one fiddle where it is working fine.
                  can you please check if your html file contains new_info element or not or if you are getting any errors in console.



                  below is my fiddle code.



                  var w = window.open('', "", "width=600, height=400, scrollbars=yes");
                  //assuming html as your info_print.html file which contains new_info element
                  var html = '<div id="new_info"></div>';
                  w.document.body.innerHTML = html;
                  w.document.getElementById("new_info").innerHTML="test content";





                  share|improve this answer













                  I have created one fiddle where it is working fine.
                  can you please check if your html file contains new_info element or not or if you are getting any errors in console.



                  below is my fiddle code.



                  var w = window.open('', "", "width=600, height=400, scrollbars=yes");
                  //assuming html as your info_print.html file which contains new_info element
                  var html = '<div id="new_info"></div>';
                  w.document.body.innerHTML = html;
                  w.document.getElementById("new_info").innerHTML="test content";






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 12:56









                  Dhaval PankhaniyaDhaval Pankhaniya

                  1,6231022




                  1,6231022






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319401%2faccess-an-element-in-a-newly-opened-window%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Error while running script in elastic search , gateway timeout

                      Adding quotations to stringified JSON object values