Saturday, May 25, 2024
 Popular · Latest · Hot · Upcoming
69
rated 0 times [  74] [ 5]  / answers: 1 / hits: 59974  / 13 Years ago, sun, may 8, 2011, 12:00:00

I found lots of functions like this one:



$(function() {
$(body a).click(function() {
alert(this.innerHTML);
return false;
});
});


What's the difference between this and $(this) in jquery?



They all have a line return false; - I don't know when I should use return false in jquery function and don't know what's the use of it?


More From » jquery

 Answers
26

According to jQuery Events: Stop (Mis)Using Return False (archived link), returning false performs three tasks when called:




  1. event.preventDefault();

  2. event.stopPropagation();

  3. Stops callback execution and returns immediately when called.



The only action needed to cancel the default behaviour is preventDefault(). Issuing return false; can create brittle code. Usually you'd want just this:



$(a).on( 'click', function (e) {
// e == our event data
e.preventDefault();
});


And secondly this is a DOM element in javascript and $(this) is a jQuery element
that references the DOM element. Read more on the topic at jQuery's this: demystified.


[#92346] Friday, May 6, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elliem

Total Points: 415
Total Questions: 117
Total Answers: 94

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
;