Grufas
Grufas

Reputation: 1450

Simple Jquery function syntax

I want to make simple Jquery function like this:

function switchButton($param) {
 $param.on('change', function() {
  var result = $($param +'.switch-field input[name=switch]:checked').val();
  alert(result);
});
}

Execute like this:

switchButton(('.switch-field input'));

Please help to format this function correctly. Thank you.

Upvotes: 1

Views: 512

Answers (1)

user3271960
user3271960

Reputation:

JSFiddle: https://jsfiddle.net/xpvt214o/111367/

function switchButton(element) {
    element.on('click', function() {
        alert(element[0].id); // do something
    });
}

switchButton($('#btn1'));
switchButton($('#btn2'));

Upvotes: 1

Related Questions