Question 1
I’ve just started learning jQuery and one thing that puzzles me is function() with a parameter in it, ie: function(e).
[Example 1]
$("#rating a").click(function(e){
// stop normal link click
e.preventDefault();
}
What is e in function(e)?
[Example 2]
$.post("test.php", function(data) {
alert("Data Loaded: " + data);
});
How do you know what data is? I assume data is different from e in example 1.
I’m confused as to why function() is different in different senarios…
Question 2
Since we are talking about basic jQuery, here’s another jQuery question. What is the difference between this and $(this)?
[Example 3]
$(document).ready(function() {
// use this to reset several forms at once
$("#reset").click(function() {
$("form").each(function() {
this.reset();
});
});
[Example 4]
$("a").hover(function(){
$(this).parents("p").addClass("highlight");
},function(){
$(this).parents("p").removeClass("highlight");
});
});
function(e) -> this is passing in the event object as a parameter. Without e you just don’t have access to the event object.
“this” is the object in context to whatever you’re doing. If you wrap “this” like $(this) then you have access to all the extensions that jQuery gives you. To be more clear, “this” is usually a DOM object, $(this) is a jQuery object.