0

I need to understand the format in which I should pass on the arguments to a specific function. I am planning to use spinner library and you can check it's demo here. I need to specify the minimum and the default values to the function spinner. The function in default mode is used as:

$('.spinnerExample').spinner()

However I need to pass on custom values for default and minimum values. The definition of the function is as follows:

(function ($) {
  $.fn.spinner = function (opts) {
    return this.each(function () {
      var defaults = {value:0, min:0}
      var options = $.extend(defaults, opts)
      var keyCodes = {up:38, down:40}
      var container = $('<div></div>')
      container.addClass('spinner')....

The link to the js file is here.

I tried $('.spinnerExample').spinner(5,2) but this does not work. I am sure the solution is simple but I dont know javascript well so I am not able to crack it.

Thanks

1 Answer 1

4

You want to use

$(".spinnerExample").spinner({value: 5, min: 2});

Note that the $.fn.spinner function only a single paramater (opts). It is pretty conventional for jQuery plugins to use an object that is merged with a set of defaults to configure the behavior of the plugin.

In this case, the defaults are {value:0, min:0}. If you want to override one or more of these values, simply pass in an object with matching keys and custom values.

More examples

$(".spinnerExample").spinner({value: 5}); // min will default to 0
$(".spinnerExample").spinner({min: 3}); // value will default to 0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.