Share via


How can I get the new focused element when focusout event is fired in a webpart?

Question

Thursday, May 3, 2012 9:02 AM

Hi all,

I use focusout event in my webpart with this code:

$('#myWebPartId').focusout( function (event) {
   alert('you have clicked out of the webpart');
});

Obviusly focusout is applied to every element in the webpart div, so focusout is fired even you click inside the webpart, and I just want to fire an event like blur who told me when my webpart has really lost the focus. Blur event doesn't work in div webpart, and I can't use events in the whole document like:

$(document).click( 
   function(e) {
      if ($(e.target).is('#myWebPartID",#myWebPartID" *')) 
        alert('focus in webpart');
});

I think the best solution would be to get the new focused element and find out if the parent is my webpart, but event.target gets the element who lost the focus don't the one who receive it, and document.activeElement doesn't really works with webparts... any idea?

All replies (2)

Thursday, May 3, 2012 12:21 PM âś…Answered

Thanks, I found what I want:

$('#myWebPartID').focusout( function () {   var tmp = $(this).has(':focus');   if(tmp.length == 0)      alert('focus out');   else     alert('focus in');});

I wanted to know when the user clicks the webpart and when he clicks out of the webpart.


Thursday, May 3, 2012 11:00 AM

What is the business requirement?

I would try wrapping the entire web part in a named div.  Then, attach the onblur() to all of the objects who are children to the named div.

Something along those lines.  It's been 6 months or so since I did this type of coding.

--Paul Galvin, Slalom
  Microsoft MVP - SharePoint
  Blogging @ http://www.mstechblogs.com/paul
  Twitter @ http://www.twitter.com/pagalvin