views:

956

answers:

2

So I've been working on this all day and I can't figure out how to get it to work. I have a table with TD's filled with content which is drawn from a database using a JQuery "getJSON" command. I have an event handler set-up so that when you double click a TD element, its contents become a INPUT element with the default value of the enclosing TD's previous contents.

The INPUT element is created inside a Javascript object named "Input" like so:

var Input = function() {
    var obj = this;
    obj.docElement = $('<input/>').attr('type', 'text').val(obj.defaultValue);
}

All of this is working so far. My problem is, I want the user to be able to hit the RETURN key while the INPUT is selected to signify they've finished editing that field. I've tried something like the following:

$(obj.docElement).bind('keydown', function(e) {
    if(e.which == 13) {
        // do something
    }
}

This works fine for the first time you edit a field; however, if you edit a field multiple times it stops working. Also if you randomly double click TD's eventually it breaks. I tested it and determined that the INPUT element stops registering any type event, as if the "bind" no longer existed on it.

I've done lots of googling and determined that the regular JQuery "bind" handler placed on an INPUT element is unreliable. Therefore I decided to attach the event handler to the document object instead using the following:

$(document).bind('keydown', function(e) {
    // do something
}

I know I can use "e.target" to get the target element that the action is executed on (and this works for me, e.target correctly refers to the INPUT element).

My question is, how do I get the object that created the INPUT element in the first place? I need to be able to execute functions contained within the corresponding "Input" class that was used to create the INPUT element. I need to call these functions from within the "$(document).bind" function. So basically I need to be able to get an INPUT element's parent/creator Input object.

If I haven't explained anything clearly enough, just let me know. Any help in this matter would be greatly appreciated! I'm also open to suggestions for alternative methods (other than using "$(document).bind").

Thanks!

A: 

I think I understand the problem ...

You can traverse the DOM to find the parent document element, but that's not what you mean, right? You want the parent script element that has a bunch of logic that operates on the element.

I suspect that it is probably easiest to provide some sort of reference to the parent when the input element is created ... pass this to the event handler, or set it in a globally accessible location (like a current_element_parent var).

Toby Hede
Thanks so much! After reading your answer I ended up deciding to use nested for-in loops to go through my object hierarchy and check each Input object's document element against the passed e.target element. Maybe not the most efficient way, but everything works now! Thanks again!
Dmart
A: 

I agree with tobyhede. You can either add a custom attribute to the INPUT element that refers back to the parent, or keep a map in memory that maps the dynamically created INPUT element to the parent that created it. When you trap the Return key, simply remove the relationship from the map so it can be added again if the user clicks it again.

Rich