Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
click trigger() not working when page loads

I am trying to make a trigger work based on the query string of the URL I use this code to get URL string

var getUrlParameter = function getUrlParameter(sParam) {
  var sPageURL = window.location.search.substring(1),
    sURLVariables = sPageURL.split('&'),
    sParameterName,
    i;

  for (i = 0; i < sURLVariables.length; i++) {
    sParameterName = sURLVariables[i].split('=');

    if (sParameterName[0] === sParam) {
      return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
    }
  }
};

and once I get it I put it inside an if then else like so:

$(document).ready(function () {
  let code_para = getUrlParameter("code");

  if (code_para == "one") {
    $("#one").trigger('click');
  } else if(code_para == "two") {
    $("#two").trigger('click');
  } else if(code_para == "three") {
    $("#three").trigger('click');
  } else if(code_para == "four") {
    $("#four").trigger('click');
  } else if(code_para == "five") {
    $("#five").trigger('click');
  } else {
    //do nothing
  }
});

but the problem is the trigger part is not working I have attached a fiddle for the full code Fiddle

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

couple of suggestions - you could simplify your logic and simply use the code_para as the identifier on the selector (assuming that the code_para is actually the same as the id of the element you want to click. Not the else block remains to do something in the avbsence of code_para).

 $(document).ready(function () {
        let code_para = getUrlParameter("code");
       if (code_para)
       {
           $("#" + code_para).trigger('click');
       } else {
           //do nothing
       }
    });

But rather than triggering a click on another DOM element - I would trigger the function that the click causes - so if these items have a function on the click - then i would simply trigger the function directly - rather than stimulating a click event that then triggers the fucntion....

about 4 years ago · Juan Pablo Isaza Relatório

0

Your issue is that clicking on #three (for example) does nothing (based on the provided fiddle).

So triggering the click, does nothing.

It's not (just) that it's too early, because the other bindings (hover and click) are on .box3, not #three. You need to trigger on .box3, eg:

$(()=> $(".box3").trigger('mouseover').trigger("click") )

So your (switch) would need to convert:

if (code_para == "one") {
    $(".box1").trigger('mouseover').trigger("click")
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda