1

I have read the jQuery documentation on .focus() as per the pset8 instructions. However, I still do not fully understand the code below:

// hide info window when text box has focus
    $("#q").focus(function(eventData) {
        info.close();
    });

Why does the provided pset8 source code input"eventData" within the anonymous function within focus()? None of the jQuery documentation examples input "eventData" into the anonymous function, as per the below example:

$( "#target" ).focus(function() {
  alert( "Handler for .focus() called." );
});

What purpose does "eventData" serve in the cs50 source code?

2

.focus(handler) is short for .on("focus", handler), and installs an event listener, in your case, handler is an anonymous function declared with one parameter.

Those handlers, when called, receive an event object. It has some properties and methods that might be interesting, in my opinion some of the most valuable are target, preventDefault, and stopPropagation. The event propagation model is really an artefact from old IE vs. Netscape battles, it goes in two directions since those browsers had different opinions on that :D

In the code you posted, eventData parameter is ignored and could be omitted.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .