Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery selector in variable

Tags:

jquery

I wanted to ask about using selector from a variable

first I have:

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

and called it in

$('input')change(check);

and they worked fine.

But now I want to pass some value to the 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 it doesn't work. Any help please.. Thanks,

nagut

like image 995
nagut Avatar asked Mar 29 '26 03:03

nagut


1 Answers

change should get a function. By writing check('.info') you are triggering the function, and passing its result to the change event. Simply wrap the call in another function:

$('input').change(function(){check('.info');});
like image 159
Kobi Avatar answered Apr 02 '26 04:04

Kobi