Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


jquery function fires on declaration

Posted on 2014-10-25
Medium Priority
Last Modified: 2014-10-26
This runs out of my "page initialized" function

function SetUpJQSliderElements() {

    var $SizeSlider = $("#SizeSlider").slider({
        min: .5,
        max: 2,
        step: .25,
        animate: "slow",

    $SizeSlider.on("slidechange", function () {
    var SizeFactorDisplay = $("#SizeFactorDisplay")

Open in new window

How might I specify the SizeSlider.on function (SizeFactorChange) so that it does not execute at the time of declaration?
Question by:codequest
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 25

Accepted Solution

Kyle Hamilton earned 2000 total points
ID: 40404810
A function declaration defines a function. What you have are two function calls (not declarations), the first one of which is an event listener:

$SizeSlider.on()  <-- function call. The on function has an event listener built into it.

.on(event, callback) <-- arguments: the event, and the callback. The callback function will will be executed when the event happens

On line 11, you have a function which listens for the "slidechange" event. Every time this event happens, the callback function will be executed. In the callback you have a function call to the SizeFactorChange function. This will execute the SizeFactorChange function.

What is the issue, and what do you  want to happen?

If you don't want it to execute, remove it.

Author Comment

ID: 40405139
Thanks, that jogged me into understanding better what was needed:

function SetUpJQSliderElements() {

    var $MapSizeSlider = $("#MapSizeSlider").slider({
        min: .5,
        max: 2,
        step: .25,
        animate: "slow",
        value: .75,
        change: MapSizeFactorChange

    //$MapSizeSlider.on("slidechange", function () {
    //    MapSizeFactorChange()
    var MapSizeFactorDisplay = $("#MapSizeFactorDisplay")

Open in new window


Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question