3

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

1
  • any errors? I can see a possible typo, $('input')change(check('.info')); is missing ., it should be $('input').change(check('.info')); Commented May 16, 2010 at 10:47

1 Answer 1

5

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');});
Sign up to request clarification or add additional context in comments.

1 Comment

Work Successfully. Thanks Kobi.

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.