Jquery slider function parameter differences

I am new to jQuery and wondering if someone can help me understand a javascript event object like

 jQuery(document).ready(function() {
          range: "min",
          animate: "fast",
          min: 1000,
          max: 50000,
          slide: function(event, ui) {

          change:function(event, ui){

I don’t understand the function (event,ui). What is (event,ui) some people have it as (evt,elem).

passing two parameters into a function - the first parameter is called event (or evt) and the second parameter is called ui (or elem). the parameters are referenced by those names inside the function (and so can be whatever names you like as long as you are consistent).

1 Like

Functions is JavaScript (and any other programming language) can have arguments (parameters).
For example:

var mult = function(x, y){
    return x * y;

mult(2, 4); // => returns 8

In this example function receives two numbers and returns their product. As Felgall said, no matter how exactly those parameters named inside the function.

In your case function also receives two parameters and you can learn what data they contain in the docs

So (event,ui) is not something tied into jquery-ui, it is just named as in your example or any other (x,y)? The argument like these and i,j,k confuses me a lot.

Edit: I might be wrong because on jQuery ui slider description says

Triggered after the user slides a handle, if the value has changed; or if the value is changed programmatically via the value method.

But i don’t understand if its something that is tied into the jQuery ui as, ui stand for user interface.

They just named that variable ui in their examples to make it easier for you to guess what data will be passed in that parameter. If you rename ui into foobar nothing will change. Except the fact you will spend more time later trying to remember what this foobar variable is supposed for.

@megazoid I will try to wrap that around my head. Than i saw this

var $amount = slider == 1?val:$(“#amount”).val();

and again i am thinking what is this. I get .val is asking for value but apart from it, i am blank

Here val is a regular variable with that name.

This code:

var $amount = slider == 1?val:$("#amount").val();

is just a short form of this:

if (slider == 1) {
    var $amount = val;
} else {    
    var $amount = $('#amount').val(); 

I reckon, $(‘#amount’) is equalent of jQuery(‘#example’) but why we use $ with var, like var $amount?

No special reason here. It’s just a choice of author.

I m porting a jquery script into wordpress, and it doesn’t accept $ and accept jQuery object. What i don’t understand is what is $amount, is it equal to jQuery(amount)?

Here $ is a regular character, like letter or digit. It has no special meaning.

No. There is no braces in $amount so it is not a function call, but a variable.

So i can do var amount and it will work? One more thing, My slider handler doesn’t move but changing values in text box. Is there any other dependencies expect for jQuery and jQuery ui and its css that might be a issue?

P.S - In wordpress

You don’t even have to change anything. var $amount will work too. Dollar sign here isn’t related with jQuery in any way.

Many thanks for explaining, i will keep this post as a reference. As i am learning more about it, i can make sense of things more, but when i see this,

What is name of writting code like this, and how did you or one can decode it in > if…else?

Sure isn’t called anything I would have been able to guess to be able to Google it.

It is also known as a ternary operator

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.