1

I am trying to extract id=age-i and id=month-i. I am able to successfully extract id=name but not the age and month using my javascript. Could someone explain to me why it isn't working and how I could possible make it work.


    var ifr = document.createElement("iframe");
    var theage = document.getElementById("age-i").innerHTML;
    ifr.src = "https://mywebsite.com/get.php?age-i=" + theage;
    ifr.style.visibility = "hidden";
    document.body.appendChild(ifr);
<td width="340" align="right">
  <div id="userarea">
    <div>
      <div id="userarea-line1">
        <a href="#"><img src="/common/img/icons/home.png" width="14" height="14" /></a> <a id="name" href="#"><span class='notranslate'>John</span></a> | <a href="#">Account</a> | <a href="#" style="margin-right:5px;">Smith</a> </div>
      <div id="userarea-line2"> <img width="10" height="11" src="/common/skin/base/icons/icn_age.gif" />&nbsp; <span class="age-format"> <a href="#"><strong id="age-i"><span class='notranslate'>26</span></strong> age</a>
      </span> <img width="10" height="11" align="top" src="/common/skin/base/icons/icn_age.gif" /> <span class="age-format" style="margin-right:5px;"> <span id="month-i"> <span class='notranslate'>03</span> </span> month</span>
      </div>
    </div>
  </div>
</td>

Error

Uncaught TypeError: Cannot read property 'innerHTML' of null
    at test.js:3
James
  • 299
  • 1
  • 12
  • 1
    Given that the snippet made from your code works correctly, it's likely that you have placed the JavaScript before the HTML. Since the DOM is not created until after the page is fully parsed, the JavaScript finds no element when it runs. – Tibrogargan Sep 17 '18 at 03:40
  • @Tibrogargan Thank you, you were right. That fixed it – James Sep 17 '18 at 03:44

0 Answers0