JQuery selector in variable

I wanted to ask about using a selector from a variable

first i:

function check()
{
  $('.info input, .info select').each(function(n, element){
     if ($(element).val()=='')
     alert('empty');
  });
}

and called him in

$('input')change(check);

and they worked great.

But now I want to pass some value to a function to make it dynamic, like

$('input')change(check('.info'));

and changed the function to

function check(sel) {   
     $(sel +' input, '+ sel + ' select').each(function(n, element){
     if ($(element).val()=='')
     alert('empty');   
  }); 
 }

but that will not work. Any help please .. Thanks,

nagut

+3
source share
1 answer

changeshould get a function. After writing check('.info'), you call the function and pass its result to the event change. Just wrap the call with another function:

$('input').change(function(){check('.info');});
+5
source

Source: https://habr.com/ru/post/1745624/


All Articles