views:

1317

answers:

8

I have a dropdown Menu where in a div is clicked and List is shown.

On focus out I am supposed to hide the list(i.e. when the user clicks or focuses on some other element and not on mouse out)

Hence my obvious choice was 'onblur' .

Now the javascript seems to work in firefox but not in IE thats because my div has a sub div with a height and width specified.

This is reproducible in a test file. I am using jQuery.

Is this a known issues in Internet Explorer ? and what is the work around?

Any suggestions?

<html>
  <head>
    <title>Exploring IE</title>
    <style type="text/css">
      /** Exploring IE**/
      .selected_option div {height:18px;}
    </style> 
    <script type="text/javascript" src="jquery-1.3.2.min9919.js"></script>
    <script type="text/javascript">
      $().ready(function(){
        $('.selected_option').blur(function(){
          alert('blurred');
        });
      });
    </script>
  </head>
  <body>
    <div class="selected_option" tabindex="0">
      <div>anywhere in the page</div>
    </div>
  </body>
</html>
A: 

Try: $('.selected_option').bind('blur', function(){
alert('blurred'); });

Also you can make another trick - handle all mouse clicks or/and focus events and if some another control is selected, then your own is blurred (ofcourse if it was selected previously).

Paul Podlipensky
+1  A: 

First realize that focus and blur events are only supported on focusable elements. To make your <div>s focusable you need to look at the tabindex property.

Crescent Fresh
A: 

I have set the tabIndex property for the div to be focusable and moreover if i comment the height the blur event is fired so I assume thats not the problem.

either edit your question or comment on someone's answer. do not post your own answer unless you figured it out and it answers your original question.
geowa4
+1  A: 

Try using an anchor tag instead of a div since these are naively focusable. You can set the href of the anchor to "javascript:void(0)" to prevent it from actually linking to a pageand use the css property "display: block" to make it render like a div. Something like this:

<html>
  <head>
    <title>Exploring IE</title>
    <style type="text/css">
      /** Exploring IE**/
      .selected_option
      {
        display: block;
        height:18px;
      }
    </style> 
    <script type="text/javascript" src="jquery-1.3.2.min9919.js"></script>
    <script type="text/javascript">
      $().ready(function(){
        $('.selected_option').blur(function(){
          alert('blurred');
        });
      });
    </script>
  </head>
  <body>
    <a href="javascript:void(0)" class="selected_option">anywhere in the page</a>
  </body>
</html>

Haven't tested this, but I think it should work.

InvisibleBacon
+2  A: 

The IE-proprietary focusout event worked for me:

$('.selected_option').bind('focusout', function(){
    alert('focusout');
});

Again, this is proprietary (see quirksmode) but may be appropriate if it solves your problem. You could always bind to both the blur and focusout events.

Nate
Thanks for the tip. This solved my problems with IE7. First tried live('focusout'), but that did not work, and then bind() which worked for ie7.
Serkan
A: 

Thanks that works for me !!

Use comments for this and mark the accepted answer.
anddoutoi
A: 

onkeypress="this.blur(); return false;"

its works fine on all ie versions

Suresh
A: 

There is no Tabindex attribute for div element

Suresh