0

How does $(this).keyup know I want the keyup listener associated with the #searchString input, and not the parent div? Doesn't $(this) represent the parent div? I dumped out $(this) to the console, and it looks like an object for the parent div.

Long story short:

How is this working? $(this).keyup

Instead of explicitly saying: $('#searchString').keyup ??

$('<div id="msg">' + <input id="searchString" name="searchString" type="text" value="" /> + '</div>').dialog({

    open: function () {

        $(this).keyup(function (e) {
            if (e.keyCode == $.ui.keyCode.ENTER)
                $('#btnFind').click();
            return false;
        })

    },

    buttons: [
        {
            text: "Find",
            id: "btnFind",
            icon: "ui-icon-search",
            click: function () {
                //do stuff
            }
        }
    ]
});
NamedArray
  • 773
  • 3
  • 10
  • 25

1 Answers1

0

Did some testing; here are my observations:

  • $(this) is representative of the parent <div id="msg">
  • $(this).keyup is targeting any (and all) extra inputs I add to <div id="msg">
  • I don't think this this is event bubbling/capturing (the listeners are not nested):
    • $('#btnFind').click(); is an action nested inside the keyup listener
    • the jQuery UI dialog button id: "btnFind" has a separate event listener, outside of the the parent <div id="msg">
  • I went ahead and explicitly indicated the target for the listener: $('#searchString').keyup

Who cares?

Well, I didn't think you could establish event listeners on inputs via the dialog open event. I'm creating my dialog on-the-fly, and my assumption was that the input may not exist while the open event was trying to establish the event listener.

NamedArray
  • 773
  • 3
  • 10
  • 25