Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
jquery selector and querySelectorAll

I tried to convert a simple function to vanilla js and the first code block is working just fine, but there is some weird thing going on and I can not seem to figure out how to get this to work. All I am trying to do here is have a direct conversion and this code is not working.

here is the original code with jquery

const obj = {
   updateProductImages() {
      var $slideshow = $('[data-product-images]')

      $('.swiper-pagination').html('');
        
      $slideshow.html('')

      this.selected_product.images.forEach((image, index) => {
         $slideshow.append(`
            <div id="Image${image.id}" class="Product__SlideItem Carousel__Cell" data-image-position="${index}" data-image-id="${image.id}">
              <div class="pw-ratio">
                <div class="pw-ratio__fill"></div> 
                <div class="pw-ratio__inner">
                  <img src="${_utils.resizeImage(image.src, 'x1600')}" />
                </div>
              </div>
            </div>
          `)
        })
   }
}

The above code works perfect, but I am refactoring a site to use only js so I can stop importing jquery.

here is my refactored code:

const obj = {
   updateProductImages() {
      //var $slideshow = $('[data-product-images]')
      const $slideshow = document.querySelectorAll('[data-product-images]')

      //$('.swiper.pagination').html('') 
      document.querySelector('.swiper-pagination').innerHTML = ''
      
      //$slideshow.html('')
      $slideshow.forEach(item => {
         item.innerHTML = ''
      })
      
      this.selected_product.images.forEach((image, index) => {
         $slideshow.forEach(item => {
            item.insertAdjacentHTML('beforeend', `
               <div id="Image${image.id}" class="Product__SlideItem Carousel__Cell" data-image-position="${index}" data-image-id="${image.id}">
                <div class="pw-ratio">
                  <div class="pw-ratio__fill"></div> 
                  <div class="pw-ratio__inner">
                    <img src="${_utils.resizeImage(image.src, 'x1600')}" />
                  </div>
                </div>
              </div>
            `)
          })
      }
   }
}

I can't believe I've spent hours on this, on something that should be an easy fix. The code in the second block does not work, I did also try using $slideshow += `...` and that did not work either.

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!