Focus on the elements inside the contenteditable div?

I am trying to use the following code to reference everything that is centered inside my contenteditable div that has an rt id:

 var lastfocused; $("#rt *").focus(function () { lastfocused = $(this); }); 

For some reason, lastfocused is always $("#rt"); but never anything that could be inside a contenteditable div. How to make everything that is concentrated inside a contenteditable div stored in the lastfocused variable?

+4
source share
2 answers

It seems your problem is elsewhere. This code works fine for me:

http://jsfiddle.net/8hZWq/

EDIT

If the children are not inputs, as you said, but the divs - the focus () method is not applicable to them, since it only works for input, text field, selection, etc.

You can also use .click () instead of focus () to save the link to the last element with a click. Keep in mind, however, this also depends on the structure of your elements.

For example, if you have several levels of containers in child divs, the #ID * selector will run several times each level of children starting with the C # ID.

If you like to store the link only to the first level of the #ID child elements, you should use the #ID > * selector to link only to direct children.

If you want to keep the link only to the element that was clicked regardless of the level associated with the container, you should use the target click link instead:

 var clicked; $('#ID').click(function(event){ clicked = $(event.target); }); 
+2
source

Indeed, your problem is that because of the function, a variable is declared. When enabled, the "lastfocused" variable will be reassigned in each focal event.

I came later, but if I come here, someone else can.

Do it:

 $("#rt *").focus(function () { var lastfocused = $(this); }); 
0
source

All Articles