Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
how to use window.onload=function with multiple jQuery click events

I have two different buttons in the navigation menu, they are anchor links to a tab switcher on the home page, which has two tabs. I need the navigation buttons to change the active tab, when clicked. It should be a simple code, but I am a newbie in JS.

I need this to call the functions after the page loads, this way, it will work from other pages too, not just from the home page.

The first function works in itself as it should. The function also works when the button is clicked on a different page. It calls the homepage, then changes the active tab after page load:

add_action( 'wp_footer', function () { ?>
<script>

jQuery('#menu-item-15507 a').on('click', window.onload=function myfunction_1() {
    jQuery( '#one-to-one a' ).click();
    return true;
});

</script>
<?php } );

But when I add the second event and function, the tabs are not changing on pageload. It works on the same page (home page), but not when fired from a different page:

add_action( 'wp_footer', function () { ?>
<script>

jQuery('#menu-item-15507 a').on('click', window.onload=function myfunction_1() {
    jQuery( '#one-to-one a' ).click();
    return true;
});

jQuery('#menu-item-16848 a').on('click', window.onload=function myfunction_2() {
    jQuery( '#courses a' ).click();
    return true;
});

</script>
<?php } );

How am I supposed to change the code to get both functions to work on page load as well?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

One problem is - don't invoke the on- setters if you need to run more than one function - use addEventListener instead.

But assigning to .onload inside a click listener doesn't make any sense anyway - by the time the user can click, the window will likely have loaded anyway.

The standard way to run something when the page is ready in jQuery is to use the main $ function's callback.

$(() => {
  const clickOne = () => $('#one-to-one a').click();
  clickOne();
  $('#menu-item-15507 a').on('click', clickOne);

  const clickCourses = () => $('#one-to-one a').click();
  clickCourses();
  $('#courses a').on('click', clickCourses);
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda