Thanks a lot You're right
now I'm trying to retrive the name of the current field but "this" seem
to be nothing e.g "{}"
Any thoughts ?
$.fn.checkform = function() {
this.bind("click", function(){
msg='';
$("input.check_empty").each(function(e){
if(this.value!='') {
$(this).css("background", "#fff");
} else {
//this.name(); //if uncommented here it fails
alert(this.toSource()); //outputs {}
msg += 'is empty\n';
$(this).css("background", "#ff0");
};
});
$("input.check_email").each(function(){
if
(this.value.search(/^\w+((-\w+)|(\.\w+))[EMAIL PROTECTED]((\.|-)\w+)*\.\w+$/) !=
-1) {
$(this).css("background", "#fff");
} else {
msg += 'is not email\n';
$(this).css("background",
"#fff0ff");
};
});
alert("Please correct:\n"+msg);
if (msg!='') return false;
else return true;
});
};
Michael Geary wrote:
$.fn.checkform = function() {
$(this).bind("click", function(){alert("it works");} );
};
What am I doing wrong ?
Ok I got it:
$.fn.checkform = function() {
return this.each(function(){
$(this).bind("click", function(){alert("it works");} );
});
};
You must be running an old version of jQuery. Your original code works in
the current release, but it wouldn't work in older versions.
Inside your checkform function, "this" is a jQuery object, not a DOM
element. So you don't need to wrap it in $(this) - although newer versions
of the jQuery code allow that. Nor do you need to run .each() on it and then
re-wrap each of the resulting DOM elements in their own $(this) wrappers -
although that works too.
Instead, you can simply code:
$.fn.checkform = function() {
this.bind("click", function(){alert("it works");} );
};
It is really confusing that "this" means different things at different times
- in a plugin function it's a jQuery object, inside a .each() callback it's
a DOM element - so it's always good to check whether "this" is what you
expect it to be. Set a breakpoint and look at "this" in the debugger, or use
an alert( this.toSource() ) in Firefox to find out what "this" really is.
-Mike
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/
|
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/