Solved

jquery function fires on declaration

Posted on 2014-10-25
2
111 Views
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",
        value:.75
    })

    $SizeSlider.on("slidechange", function () {
        SizeFactorChange()
    })
    
    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?
0
Comment
Question by:codequest
2 Comments
 
LVL 25

Accepted Solution

by:
Kyle Hamilton earned 500 total points
Comment Utility
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.
0
 
LVL 2

Author Comment

by:codequest
Comment Utility
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

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now