Trying to prevent Javascript from running on page load

TargetofGravity

I'm trying to add an onChange event handler to two element of my html page. Sadly the event is firing when the page loads which is causing downstream issues.

$("#Division").on('click', onChangeFilter());
$("#Program").change(onChangeFilter());

function onChangeFilter() {
   alert("Fired to soon....");
};

The associated HTML for testing looks like this

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
</head>
<body>
   <div id="DivisionSelection">
     <select id="Division" data-filterprogramcontrol="Program">
       <option value="-1">Select Division</option>
       <option value="1">Item 1</option>
       <option value="2">Item 2</option>
       <option value="3">Item 3</option>
     </select>
  </div>    
</body>
</html>

Is adding the .change event causing a 'change' and triggering the function? The two closest posts I was able to find are located here and here but I feel their issues were more transparent. Nothing was obvious to me in the jQuery API indicating this behavior. I just started transitioning to the web world in my course work, maybe I'm not understanding correctly the dynamics of how a page loads the DOM. I thought this page useful but not sure how accurate. Thank you in advance for the guidance.

Jonathan Lonowski

You'll want to remove the invoking parenthesis after each use of onChangeFilter:

$("#Division").on('click', onChangeFilter);
//                                      ^^
$("#Program").change(onChangeFilter);
//                                ^^

This treats the function as any other (object) value, passing it as an argument to .on() and .change() for them to use and for the events to invoke when they've been triggered.

Otherwise, with the parenthesis, the function is being called first and its return value is instead passed to .on() and .change(), similar to:

var result1 = onChangeFilter();
$("#Division").on('click', result1);

var result2 = onChangeFilter();
$("#Program").change(result2);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Running Javascript function with Shopify on page load

From Dev

Prevent JavaScript function from running twice (setTimeout)

From Dev

Prevent page from loading

From Dev

Is there any way to prevent page load of images with javascript?

From Dev

How do I prevent a global javascript variable from initializing on every page load?

From Dev

Running a javascript function on page load

From Dev

Prevent sub from running

From Dev

How to fix "page trying to load scripts from unauthenticated source"

From Dev

Prevent to load a page with js disabled

From Dev

errors prevent embedded javascript from running

From Dev

How to prevent from page load to clear my dynamic added textboxes

From Dev

prevent backup from running in parallel

From Dev

What methods are there to prevent the Javascript gc from running as little as possible?

From Dev

Prevent javascript component from refreshing my entire page

From Dev

Load JavaScript model from fields on page

From Dev

How to prevent showing the elements of the page at the page load

From Dev

addEventListener is running function on page load only javascript

From Dev

How do I prevent a global javascript variable from initializing on every page load?

From Dev

Running a javascript function on page load

From Dev

Why is this function not running on page load?

From Dev

How to prevent R from trying to load mgcv

From Dev

Trying to load a response sheet from php in the same page

From Dev

('checkbox').change() running on page load

From Dev

Chrome - Prevent scripts from running in a page

From Dev

Trying to prevent duplicate item on page

From Dev

How to prevent Knockout.js function from running on page load?

From Dev

Load Javascript on page load

From Dev

JHipster Nginx HTTPS This page is trying to load scripts from unauthenticated sources

From Dev

Prevent page from exiting

Related Related

  1. 1

    Running Javascript function with Shopify on page load

  2. 2

    Prevent JavaScript function from running twice (setTimeout)

  3. 3

    Prevent page from loading

  4. 4

    Is there any way to prevent page load of images with javascript?

  5. 5

    How do I prevent a global javascript variable from initializing on every page load?

  6. 6

    Running a javascript function on page load

  7. 7

    Prevent sub from running

  8. 8

    How to fix "page trying to load scripts from unauthenticated source"

  9. 9

    Prevent to load a page with js disabled

  10. 10

    errors prevent embedded javascript from running

  11. 11

    How to prevent from page load to clear my dynamic added textboxes

  12. 12

    prevent backup from running in parallel

  13. 13

    What methods are there to prevent the Javascript gc from running as little as possible?

  14. 14

    Prevent javascript component from refreshing my entire page

  15. 15

    Load JavaScript model from fields on page

  16. 16

    How to prevent showing the elements of the page at the page load

  17. 17

    addEventListener is running function on page load only javascript

  18. 18

    How do I prevent a global javascript variable from initializing on every page load?

  19. 19

    Running a javascript function on page load

  20. 20

    Why is this function not running on page load?

  21. 21

    How to prevent R from trying to load mgcv

  22. 22

    Trying to load a response sheet from php in the same page

  23. 23

    ('checkbox').change() running on page load

  24. 24

    Chrome - Prevent scripts from running in a page

  25. 25

    Trying to prevent duplicate item on page

  26. 26

    How to prevent Knockout.js function from running on page load?

  27. 27

    Load Javascript on page load

  28. 28

    JHipster Nginx HTTPS This page is trying to load scripts from unauthenticated sources

  29. 29

    Prevent page from exiting

HotTag

Archive