TransWikia.com

HTML Template not rendering on screen

Stack Overflow Asked by OJM on December 6, 2020

I’m currently building a Twitter clone for my portfolio using Web Component’s template feature. However, my template isn’t rendering on screen when the tweet button is clicked. I’m currently getting an error ‘Uncaught TypeError: Cannot set property ‘innerHTML’ of null at HTMLButtonElement.postTweetButton.onclick‘.

I have attempted moving the ‘tweetText.innerHTML = tweetBoxInput;’ line around to no avail. My tweetText variable’s value is correct so I’m not sure why the property cannot set. I suspect it’s to do with the template not importing correctly.

JS FIDDLE: https://jsfiddle.net/manoj1234/mty68qng/13/

Help greatly appreciated. Thanks.

JS:

window.onload = () => {

  const createTweetContainer = document.getElementById("createTweetContainer");

  const createTweetButton = document.getElementById("createTweetButton");
  const backArrow = document.getElementById("backArrow");

  const tweetBox = document.getElementById("tweetBox");

  let tweetBoxInput;

  const pinnedTweet = document.getElementById("pinnedTweet");
  const tweetContainer = document.getElementById("tweetContainer");

  const tweetSentContainer = document.getElementById("tweetSentContainer");



    createTweetButton.onclick = () => {
    createTweetContainer.style.display = "block";
    tweetBox.value = "";
  }

  backArrow.onclick = () => {
    createTweetContainer.style.display = "none";
  }


  postTweetButton.onclick = () => {

    var tweetText = document.getElementById("tweetText");


    console.log(tweetBox.value);

    tweetBoxInput = tweetBox.value;


    if (tweetBoxInput == "") {
      console.log("please write tweet");
    } else {

      createTweetContainer.style.display = "none";
      tweetSentContainer.style.display = "flex";

      var tweetTemplate = document.getElementById("tweet-template");


      var tweetInstance = document.importNode(tweetTemplate.content, true);



      tweetText.innerHTML = tweetBoxInput;

      pinnedTweet.after(tweetInstance);



      /* Show Tweet Sent container*/
      setTimeout(() => {
        tweetSentContainer.style.height = "30px";
      }, 1000);

      setTimeout(() => {
        tweetSentContainer.style.opacity = "1";

      }, 1300);

      /* End of Show Tweet Sent container */

      /* Hide Tweet Sent container */

      setTimeout(() => {
        tweetSentContainer.style.opacity = "0";
      }, 5000);

      setTimeout(() => {
        tweetSentContainer.style.height = "0";
        tweetSentContainer.style.marginBottom = "0";
      }, 5300);

      setTimeout(() => {
        tweetSentContainer.style.display = "none";
        tweetSentContainer.style.marginBottom = "12px";
      }, 8000);

      /*End of Hide Tweet Sent container */
    }

 }

}

CSS:

  * {
     margin: 0;
     padding: 0;
     transition: ease 0.2s;
     box-sizing: border-box;
     -webkit-user-drag: none;
     appearance: none;
     outline: none;
     font-family: 'Roboto';
}
 body {
     display: flex;
     justify-content: center;
     align-items: center;
}
 body main {
     width: 480px;
    /*background-color: $blueBackground;
     */
}
 .globalWrap {
     padding-left: 25px;
     padding-right: 25px;
}
 .greyText {
     color: #8997a2;
     font-weight: normal;
}
 .bodyText {
     font-size: 15px;
}
 #bottomFixed {
     width: 100%;
     position: fixed;
     bottom: 0;
     z-index: 4;
     display: flex;
     align-items: flex-end;
     flex-direction: column;
}
 #bottomFixed #createTweetButton {
     width: 65px;
     height: 65px;
     font-size: 1.2em;
     display: flex;
     justify-content: center;
     align-items: center;
     color: white;
     border-radius: 360px;
     background-color: #1da1f2;
     margin-bottom: 12px;
     margin-right: 10px;
}
 #bottomFixed #navBar {
     height: 50px;
     width: 100%;
     background-color: #15202b;
     border-top: solid 1px #38444d;
}
 #bottomFixed #tweetSentContainer {
     height: 30px;
     height: 0px;
     bottom: 20;
     z-index: 6;
     background-color: #1da1f2;
     width: 80%;
     display: flex;
     align-self: center;
     display: none;
     opacity: 0;
     justify-content: space-between;
     align-items: center;
     padding-left: 25px;
     padding-right: 25px;
     margin-bottom: 12px;
     border-radius: 5px;
     transition: ease-in-out 0.3s;
}
 #coverImgContainer {
     height: 125px;
     width: 100%;
}
 #coverImgContainer img {
     height: 100%;
     width: 100%;
     position: relative;
     object-fit: cover;
}
 #userProfile {
     width: 100%;
     background-color: #15202b;
     color: white;
}
 #userProfile #userImgContainer {
     display: flex;
     justify-content: space-between;
     align-items: flex-start;
}
 #userProfile #userImgContainer button {
     margin-top: 12px;
     background-color: transparent;
     border: solid 1px #1da1f2;
     padding-left: 12px;
     padding-right: 12px;
     padding-top: 6px;
     padding-bottom: 6px;
     border-radius: 25px;
     color: #1da1f2;
     font-size: 0.8em;
}
 #userProfile #userImgContainer h2 {
     font-size: 1em;
}
 #userProfile #userImgContainer #profileImgName {
     position: relative;
     bottom: 25;
}
 #userProfile #profilePicContainer {
     height: 80px;
     width: 80px;
}
 #userProfile #profilePicContainer img {
     width: 100%;
     border-radius: 100%;
     border: solid 4px #15202b;
}
 #userProfile #profileNav {
     display: flex;
     justify-content: space-between;
     margin-top: 16px;
}
 #userProfile #profileNav h4 {
     padding-left: 10px;
     padding-right: 10px;
     padding-bottom: 12px;
     font-size: 16px;
     color: #8997a2;
}
 #userProfile #profileNav h4:nth-child(1) {
     color: #1da1f2;
     border-bottom: solid 2px #1da1f2;
     padding-left: 25px;
     padding-right: 25px;
}
 #userProfile #profileNav h4:nth-child(4) {
     padding-right: 25px;
}
 #userProfile #userInfo p:nth-child(1) {
     position: relative;
     bottom: 12.5;
}
 #userProfile #userInfo p:nth-child(2) {
     position: relative;
     bottom: 6.25;
     display: flex;
}
 #userProfile #userInfo p:nth-child(3) {
     font-weight: bold;
     margin-top: 4px;
     font-size: 14px;
}
 #userProfile #userInfo span {
     font-weight: normal;
}
 #userProfile #userInfo span:nth-child(1) {
     margin-right: 6px;
}
 #userProfile #userInfo svg {
     width: 5%;
     color: #8997a2;
     fill: #8997a2;
     margin-right: 5px;
     display: none;
}
 #timelineContainer {
     background-color: #12161e;
     height: 100vh;
     width: 100%;
}
 .tweetContainer {
     display: flex;
     padding-top: 12px;
     padding-bottom: 12px;
     padding-left: 25px;
     padding-right: 25px;
     width: 100%;
     background-color: #15202b;
     border-top: solid 1px #38444d;
     transition: ease-in-out 0.3s;
}
 .tweetContainer .tweetName {
     color: white;
}
 .tweetContainer .tweetText {
     color: white;
     margin-bottom: 12px;
}
 .tweetContainer #tweetText {
     color: white;
     margin-bottom: 12px;
}
 .tweetContainer .tweetImgContainer {
     width: 100%;
     height: 200px;
     display: flex;
     border-radius: 12px;
     overflow: hidden;
    /*Add this to main container so the Border-Radius also rounds child elements*/
     border: solid 1px #38444d;
}
 .tweetContainer .tweetImgContainer #col-1ImgContainer {
     width: 100%;
     overflow: hidden;
}
 .tweetContainer .tweetImgContainer #col-1ImgContainer img {
     height: 100%;
     width: 100%;
     object-fit: cover;
     border-right: solid 5px #12161e;
}
 .tweetContainer .tweetImgContainer .col-2ImgContainer {
     height: 50%;
     overflow: hidden;
}
 .tweetContainer .tweetImgContainer .col-2ImgContainer:nth-child(1) {
     border-bottom: solid 5px #12161e;
}
 .tweetContainer .tweetImgContainer .col-2ImgContainer img {
     transform: scale(1.5, 1.5);
     height: 100%;
     width: 100%;
     object-fit: cover;
}
 .tweetProfileImgContainer {
     min-width: 55px;
     min-height: 55px;
     max-width: 55px;
     max-height: 55px;
     padding-right: 12px;
}
 .tweetProfileImgContainer .tweetProfileImg {
     width: 100%;
     border-radius: 100%;
}
/* Create Tweet Page */
 #createTweetContainer {
     height: 100vh;
     width: 100%;
     background-color: #15202b;
     position: fixed;
     z-index: 5;
     display: none;
}
 #createTweetContainer img {
     margin-right: 12px;
     margin-left: 25px;
}
 #createTweetContainer #createTweetHeader {
     height: 45px;
     border-bottom: solid 1px #38444d;
     color: white;
     display: flex;
     justify-content: space-between;
     align-items: center;
     padding-left: 25px;
     padding-right: 25px;
}
 #createTweetContainer #createTweetHeader i {
     color: #1da1f2;
}
 #createTweetContainer #createTweetHeader button {
     background-color: #1da1f2;
     border: solid 1px #1da1f2;
     padding-left: 24px;
     padding-right: 24px;
     padding-top: 6px;
     padding-bottom: 6px;
     border-radius: 25px;
     color: white;
     font-size: 0.8em;
     font-weight: bold;
     opacity: 0.5;
}
 #profileTweetBoxContainer {
     display: flex;
     justify-content: space-between;
     padding-top: 12px;
}
 textarea {
     margin-left: 25px;
     width: 100%;
     resize: none;
     background-color: #15202b;
     border: 0;
     color: white;
     outline: none;
     padding-right: 25px;
     font-family: Arial, Helvetica, sans-serif;
     font-size: 16px;
     position: relative;
     top: 10;
     caret-color: #1da1f2;
}

HTML:

  <script src="https://kit.fontawesome.com/cd801faa65.js" crossorigin="anonymous"></script>
<div id="bottomFixed">

  <div id="createTweetButton">
    <i class="fas fa-feather"></i>
  </div>

  <div id="tweetSentContainer">
    <p><i class="fas fa-check-circle"></i>Your Tweet was sent.</p>
    <p>View</p>
  </div>

  <div id="navBar">
  </div>



</div>



<section id="createTweetContainer">

  <div id="createTweetHeader">
    <i id="backArrow" class="fas fa-arrow-left"></i>
    <button id="postTweetButton">Tweet</button>
  </div>

  <div id="profileTweetBoxContainer">

    <figure class="tweetProfileImgContainer">
      <img class="tweetProfileImg" src="https://cdn.pixabay.com/photo/2016/11/08/15/21/user-1808597_1280.png">
    </figure>

    <textarea id="tweetBox" cols="500" rows="10" placeholder="What's Happening?"></textarea>

  </div>


</section>







<section id="timelineContainer">


  <div id="pinnedTweet" class="tweetContainer">

    <figure class="tweetProfileImgContainer">
      <img class="tweetProfileImg" src="https://cdn.pixabay.com/photo/2016/11/08/15/21/user-1808597_1280.png">
    </figure>

    <div>

      <h4 class="tweetName bodyText">Name <span class="greyText">@username</span></h4>

      <p class="tweetText bodyText">Tweet Text Here</p>


      <div class="tweetImgContainer">

        <div id="col-1ImgContainer">
          <img src="https://cdn.getyourguide.com/img/tour/5ac513c518061.jpeg/146.jpg">
        </div>
      </div>

    </div>


  </div>



  <template id="tweet-template">


    <div id="tweetContainer" class="tweetContainer">
      <figure class="tweetProfileImgContainer">
        <img class="tweetProfileImg" src="images/profilepicture.jpg">
      </figure>

      <div>
        <h4 class="tweetName">Emmanuel</h4>

        <p id="tweetText"></p>


      </div>
    </div>

  </template>






</section>
 

 

 

One Answer

I realised I was attempting to change the original template's p tag rather than the imported node. I added tweetInstance.querySelectorAll(‘p’)[0].innerHTML = tweetBoxInput; which changes the text of the new node.

Correct answer by OJM on December 6, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP