Jump to content
Larry Ullman's Book Forums
davimagn

P292 | Chapter 8 | Event Handling | Event Properties | Blur/Focus Question

Recommended Posts

This book is the best. It's cracking a few Javascript nuts for me.

 

I have arrived fairly unscathed, at Chapter 8.

 

My question relates to the form (figure 8.7) which helps demonstrate the use of the event object.

 

The 'blur' event doesn't seem to do anything. I tried adding 'focus' and that didn't do anything either. I assumed a coding fault on my part and downloaded the book script and it still doesn't work.

 

My understanding would be that clicking in an out of the textarea would be a focus/blur event, I added another input field in case, but no. Why?

 

Thank you,

Share this post


Link to post
Share on other sites

Try running the following code from Chrome with the console open, and then click in and out of the text area:

 

 

<!DOCTYPE html>
 
<html lang="en">
  
  <head>
    
    <title>Focus/Blur test</title>
    
    <meta charset="UTF-8">
    
  </head>
  
  <body>
    
    <textarea id="ta"></textarea>
    
    <script>
      
      document.getElementById('ta').onfocus = function () {
        
        console.log('Text area focus');
        
      };
      
      document.getElementById('ta').onblur = function () {
        
        console.log('Text area blur');
        
      };
      
    </script>
    
  </body>
  
</html>

Share this post


Link to post
Share on other sites

Thank you HartleySan, I appreciate the time and thought you have taken in order to put together your reply. And I can clearly reason from your code. Thank you.

 

But more precisely my query is more specific in the case of the form Larry Ullman uses in this book - the working example doesn't show the blur / focus events in the output (when checkboxes are checked), and I wondered if there was a reason why this was? Is there a problem in his own code?

Share this post


Link to post
Share on other sites

I don't think there's a problem with his code.

 

While there may be slight variations browser to browser, as far as I know, the onfocus and onblur events fire for checkboxes only when the Tab key (and not the mouse) is used.

I would recommend some testing to confirm that though.

More than likely, an onclick event would be much better for detecting "focus" when a checkbox is clicked on.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...