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

161
Views
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 answers
Answer question

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 Report
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!