Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert anonymous function to a regular one?

Total JS noob here. I have the following line that implements the jQuery Slider:

<script type="text/javascript">
    $(document).ready(function () {
        $("#wheelLeft").slider({ 
                 orientation: 'vertical', value: 37, 
                 min: -100, max: 100, 
                 slide: function (event, ui) { $("#lblInfo").text("left"); } });
    });
</script>

Basically on the slide event, the #lblInfo gets its text set to left. This works fine. However, I'd like to convert the inline anonymous function that handles the slide event into a regular function.

Can someone help out?

like image 470
AngryHacker Avatar asked Jan 31 '11 21:01

AngryHacker


3 Answers

<script type="text/javascript">

function handleSlide(event, ui) { 
    $("#lblInfo").text("left"); 
}

$(document).ready(function () {
    $("#wheelLeft").slider({ 
             orientation: 'vertical', value: 37, 
             min: -100, max: 100, 
             slide: handleSlide
    });
});
</script>
like image 79
Dylan Beattie Avatar answered Nov 02 '22 17:11

Dylan Beattie


<script type="text/javascript">
    function myfunc(event, ui) {

    }

    $(document).ready(function () {
        myfunc(event, ui)
    });
</script>

would do it. It's obviously not an ideal solution as it still uses the anonymous but should solve any issues you have where you need to manipulate the function manually

like image 29
Basic Avatar answered Nov 02 '22 18:11

Basic


Just define a named function:

function doSlide(event, ui)
{
    $("#lblInfo").text("left");
}

$(document).ready(function() {
    $("#wheelLeft").slider({
        orientation: 'vertical', value: 37, 
        min: -100, max: 100, 
        slide: doSlide
    });
});
like image 2
Frédéric Hamidi Avatar answered Nov 02 '22 16:11

Frédéric Hamidi