<input type="text" id="text">
<span id="click">click</span>
$("#click").click(function(){
if($("#text").val().length < 1){
alert("click is empty!");
false;
}
alert("ok");
})
LIVE: http://jsfiddle.net/2Be8a/
Why in this example if $click length == 0 i have alert click is empty (this is good) and next alert ok (not good) – why false doesnt stop script?
I know – i can use else, but is possible stop with false?
You need to use the key word
returnto exit the function.Additional Information
return.As suggested in the comments, it would be “best” to add an
elsethat returns true. Example:Essentially, in a jQuery handler,
return trueis the same as returning nothing (jQuery will take no action).return falseis the equivalent ofevent.stopPropagation().I suggest reading jQuery Events: Stop (Mis)Using Return False to gain a better understanding of what’s really going on when you use
return false.