November 29, 201312 yr 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'); } });
November 30, 201312 yr 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