Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

jQuery Question

Featured Replies

I'm literally brand new to jQuery and have been playing around, so far good. Until now, I have a fade in div which contains a form and it fades out when clicked anywhere else on the page, this includes the actual forms, so i can't fill the form out before it fades out.

 

My question is how do i fix it so that my form will let me fill in the fields, but will fade out when clicked any where else on the page.

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />

<title></title>    
    
<link rel="stylesheet" href="style.css">
<link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
    
</head>
<body>
    
    <div id="wrapper"><!-- START OF WRAPPER -->
        
        <header></header>
        
        <nav></nav>
        
        <div id="infobar">
        
            <ul>
            <li><a id="log" href="#">LOGIN</a></li>
            </ul>
        
        </div>
        
        <div id="loginform">
        
        <form>
            
            <input id="user" type="text" name="user" placeholder="Username" />
            <input id="pass" type="password"  name="pass" placeholder="Password" /><br />
            <button id="loginbut">Login</button>
            
        </form>
            
            <p id="forgot">Forgotten Password</p>
        
        </div>
        
            <aside></aside>
            
            <section></section>
        
        <footer></footer>
        
    </div><!-- END OF WRAPPER -->
    
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="click.js"></script>
    
</body>
</html>
$('#log').click(function() {  
    $('#loginform').fadeIn('slow');
    
});


$(document).on('click', ':not(#log)', function(e){ 
  if(e.target.id !== 'log') {
      $("#loginform").fadeOut('slow');
      
        }
}); 

 

You have assigned the click event to the entire document this will affect all child elements including your form.

The easiest way to achieve what you want is to use the wrapper as selector instead and place the form outside the wrapper.

You will need to set the forms position to absolute in order to position it correctly.

$(#wrapper).on('click', ':not(#log)', function(e){ 
  if(e.target.id !== 'log') {
      $("#loginform").fadeOut('slow');
      
        }
}); 

However you should be careful about assigning click-events to elements that are usually not clickable it might be more semantic to have a close-button as users would click the document for other purposes like getting the window in focus ect.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.