Error message: "TypeError when trying to append a child, even though the

I am encountering an issue with Shopify where I need to sort blog posts by a date different from the publish date. According to the Shopify Community Forums, the most effective way to achieve this is by utilizing liquid to input the information and then modifying it through client-side Javascript. My approach involves placing the div and the new date into an object within an array. This object is structured to contain a div value and a date value in the following format:

0: 
date: "1563681600" 
div: " <div class="article-image">
          <a href="/blogs/upcoming/dog-days-of-summer">
            <div class="rimage-outer-wrapper" style="max-width: 750px"><div class="rimage-wrapper lazyload--placeholder" style="padding-top:60.0%"><img class="rimage__image lazyload fade-in " data-src="//cdn2.shopify.com/s/files/1/0152/7041/2388/articles/84th_2019-0721_slider_{width}x.jpg?v=1561562008" data-widths="[180, 220, 300, 360, 460, 540, 720, 900, 1080, 1296, 1512, 1728, 2048]" data-aspectratio="1.6666666666666667" data-sizes="auto" alt="DOG DAYS OF SUMMER"><noscript><img src="//cdn2.shopify.com/s/files/1/0152/7041/2388/articles/84th_2019-0721_slider_1024x1024.jpg?v=1561562008" alt="DOG DAYS OF SUMMER" class="rimage__image"></noscript></div></div>

          </a>
        </div>


        <div class="meta">

                  <span class="meta-item date">1563681600</span>
                  <span class="meta-item" id="time">1-3pm</span>
                  <span class="meta-item location">84th Street Location</span>


        </div>

        <h2><a href="/blogs/upcoming/dog-days-of-summer" title="">DOG DAYS OF SUMMER</a></h2>
        <div class="rte article-excerpt"><b>Books of Wonder</b><span>&nbsp;</span><span>is excited to host a picture book event: DOG DAYS OF SUMMER. Please join us on Sunday, July 21st at our 84th Street Store to celebrate four amazing picture books about dogs!</span></div>

        <p class="fullarticle"><a class="cta-link" href="/blogs/upcoming/dog-days-of-summer">Read more</a></p>
      " 

After successfully sorting the array by date, my current challenge lies in displaying these div elements in the DOM. Despite capturing these DIVS using innerHTML, I encounter an error when attempting to use AppendChild. Here is the code snippet:

function storeid() {
  var divName = document.getElementsByClassName('pullup');
  var className = document.getElementsByClassName('date');
  var classnameCount = className.length;
  var IdStore = new Array();

  for (var j = 0; j < classnameCount; j++) {
    var obj = {};
    var div = divName[j].innerHTML;
    var dateString = className[j].innerText;
    obj["div"] = div;
    obj["date"] = dateString;
    IdStore.push(obj);
  }
  console.log(IdStore);
  IdStore = IdStore.sort(function(a, b) {
    return b.date - a.date
  });

  IdStore.forEach(el => {
    document.body.appendChild(el.div);
  })
}
storeid();

Despite specifying that only the div should be appended, I continue to receive the error message:

TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'

Could appendChild possibly be unable to handle this much content? Or is there a more efficient solution to this problem?

Answer №1

Once you perform the following action:

var div = divName[j].innerHTML;

You are no longer working with a Node, but rather a string. To fix this, simply delete the .innerHTML from the line.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Loop through a collection of arrays that contain the same elements, and multiply each consecutive element by a specified value x

I've been diving into a challenging problem involving remarkable numbers, which are defined as A number that is equal to the sum of all its proper divisors -- provided one of them is negative. For instance, the proper divisors of 12 are 1, 2, 3, 4, 6 ...

Using Selenium WebDriver and JavaScript: Enabling Chrome to Download Multiple Files at Once

After scouring through multiple documents for hours like https://www.selenium.dev/documentation/en/webdriver/js_alerts_prompts_and_confirmations/ as well as https://chromedriver.chromium.org/capabilities and I was unsuccessful in finding a solution wit ...

Unable to open modals in Bootstrap using jQuery script: modal not popping up

My attempt to create functionality for two buttons and two modals - reserve a campsite (id="reserveButton" should open id="reserveModal") and Log in (id="loginButton" should open id="loginModal") is not working. I ha ...

Utilizing AngularJS for toggling data with ng-click

In my project, I have a unique issue with a list item that toggles a modal and sets a parameter using ng-click. Strangely, when calling a specific function in another location, Course.SelectedCourse returns as undefined, even though Course.ID has a valid ...

A guide on utilizing webpack devServer proxy within a create react app

Currently, I am in the process of developing a new application with create-react-app and I am looking to incorporate some proxies into my code. In the past, I utilized webpack's devServer for this purpose. module.exports = { ... devServer: { ...

The ability to rate the product in Livewire:Laravel is exclusively reserved for buyers

I have integrated the Livewire rating system into my Laravel e-commerce platform. Currently, all users can rate and comment on any product. However, I want to restrict this privilege to only buyers. ProductRatings.php class ProductRatings extends Componen ...

Google Maps does not support markers appearing on the map

I have created a basic web application that displays markers from a MySQL database on Google Maps using a table called markers_titik. In order to process this data, I have written a simple PHP script named map_process.php. Here is the code: <?php //PH ...

How can I adjust the image size based on the screen dimensions?

As a newcomer to React-Native and Javascript, I am currently in the process of learning. My goal is to adjust an image's width to be one-tenth of the screen size. I have set up a constructor and a state to retrieve the screen width and height, but I a ...

Identifying selected shapes in CSS/Javascript by outlining them with small rectangles placed on each corner

Currently, I am working on a University Project that involves creating a selection function for drawn shapes such as rectangles, circles, lines, or triangles. The challenge lies in figuring out the visualization of the selection when a shape is clicked. T ...

Place the retrieved data from the API directly into the editor

I've integrated the LineControl Editor into my app and everything is functioning perfectly, except for when I attempt to insert text into the editor. Here's the link to the LineControl GitHub page: https://github.com/suyati/line-control/wiki Fo ...

Managing both clicking and hovering events on a single element, ensuring that the popup modal remains open as long as it is being hovered over

After successfully implementing click and hover functionality on an element, I was able to position the popup relative to the mouse pointer based on a previous solution. However, I am now facing an issue where I want the popup modal to be fixed in a specif ...

Performing matrix manipulations using Mirror.js (Three.js)

I am currently working on creating water effects in three.js and I haven't come across any examples in three.js that incorporate both reflection and refraction. If you know of any examples, please feel free to share the links with me. Currently, I am ...

Implementing drag-and-drop functionality for text on an image using javascript

Hey there, I'm looking to develop a super simple meme creator tool. I have some code for adding text to an image and dragging it around. Can anyone help me out? <html> <body> <div id="draggable-element">Drag me!</div> <styl ...

Redux action intensifies upon socket events

I have a Reactjs and Redux application that works well with axios. However, I would like to integrate socket.io into it. In my redux action, I tried the following: export const getOne = (id) => async dispatch => { socket.emit('getOne', i ...

JavaScript onClick event not functioning properly on iOS devices

I have created a code that can detect when a user clicks on a cell in a table and retrieves the background color set for that cell. Everything works perfectly on my desktop computer, but when I attempt to use my iPad, it does not respond. I attempted to u ...

Can Javascript work together with HTML?

Can 'this' be utilized in this specific scenario? var td = document.getElementsByTagName("td"); for (var i = 0; i<td.length; i++){ td[i].id = 'abc' + i; }; for (var i = 0; i<td.length; i++){ td[i].onclick = c ...

Is it no longer possible to use v-for to iterate over translations in Nuxt 3 and @nuxtjs/i18n?

Imagine having an Array stored in our translations file en.js section: { title: 'This value is a string and it works perfectly', highlighted: [ { title: 'Highlighted title 1', text: 'Highlighted text ...

Applying conditional statements to an array's parent elements during iterations in JavaScript

Here is my complete script for relevance. I am currently looping through an array from a JSON object and populating content into an HTML div based on the content's ID. The process works well, but I am facing an issue with the logic related to the par ...

Deactivate the button when submitting the form

Is there a way to disable a button on a form submission in order to prevent users from submitting multiple times? I attempted to disable the button with JavaScript using onclick, but encountered an issue where the button remained disabled if client side v ...

What could be causing the server to return an empty response to an ajax HTTP POST request?

Attempting to make a POST request using ajax in the following manner: $.ajax({ type: "POST", url: 'http://192.168.1.140/', data: "{}", dataType: "json", ...