Run code after first lines

Asked

Viewed 27 times

1

Good afternoon, you guys! I’m trying to solve a little bug here from a scritp I made for a site, I’m beginner yet and I’m not able to solve.

What happens is: I need the code to run only after this line because there is no time for the elements to run

$( "#filtro-belem" ).load( "http://recursos.belempneusb2b.ecommercestore.com.br/c/filtro.html" );
$( "#filtro-belem" ).load( "http://recursos.belempneusb2b.ecommercestore.com.br/c/filtro.html" );

(function() {
const category = document.getElementById("category")
const width = document.getElementById("width")
const profile = document.getElementById("profile")
const wheel = document.getElementById("wheel")

const nextTypes = {
  category: width,
  width: profile,
  profile: wheel
}

function handleFilterType(type) {
    return () => {
        const { id, value } = type
        const currentOption = type.querySelector(`option[value='${value}']`).innerText

        const nextType = nextTypes[id]
        const options = nextType.querySelectorAll("option")

        options.forEach((option) => {
          if (!option.dataset || !option.dataset.relation) return null
          const acceptedTypes = option.dataset.relation.split("-")

          option.hidden = !acceptedTypes.includes(currentOption)
        })
    }
}

category.addEventListener("change", handleFilterType(category))
width.addEventListener("change", handleFilterType(width))
profile.addEventListener("change", handleFilterType(profile))
})();

1 answer

1


That method .load() accepts a function as the second or third argument that will use as callback when the .load ran.

So instead of using that IIFE you can put that code inside that callback:

$("#filtro-belem").load("http://recursos.belempneusb2b.ecommercestore.com.br/c/filtro.html", function() {
  const category = document.getElementById("category")
  const width = document.getElementById("width")
  const profile = document.getElementById("profile")
  const wheel = document.getElementById("wheel")

  const nextTypes = {
    category: width,
    width: profile,
    profile: wheel
  }

  function handleFilterType(type) {
    return () => {
      const {
        id,
        value
      } = type
      const currentOption = type.querySelector(`option[value='${value}']`).innerText

      const nextType = nextTypes[id]
      const options = nextType.querySelectorAll("option")

      options.forEach((option) => {
        if (!option.dataset || !option.dataset.relation) return null
        const acceptedTypes = option.dataset.relation.split("-")

        option.hidden = !acceptedTypes.includes(currentOption)
      })
    }
  }

  category.addEventListener("change", handleFilterType(category))
  width.addEventListener("change", handleFilterType(width))
  profile.addEventListener("change", handleFilterType(profile))
});
  • Wow, thank you so much! It worked perfectly! If it’s not too much to ask, I would have one more question. I have two codes of this on the same page. But JS is only applying to one of them. Would you have any idea how I can do?

  • @Kaioalmeida are both pointed to #filtro-belem? or the other has another selector?

  • The two are marked with #filter-Belem. I have this same html on other pages. I’m trying to solve this part trying to pull with JS or something so I don’t have to copy the same code to the other pages, but one step at a time

  • @Kaioalmeida if you have both pointing to #filtro-belem they will overwrite each other... Ids have to be unique in HTML. Puts another ID instead of #filtro-belem...

  • Wow, no. I got it wrong anyway.. Actually, what’s being pulled twice is the.html filter file. JS is loading several times the page. Sorry for my error

Browser other questions tagged

You are not signed in. Login or sign up in order to post.