javascript - Trying to prevent default -


this project working on, little popup ad client's website tell users summer camps coming up. want ad show on load, , user can choose close ad or click link go registration page.

i trying code work problem when click 1 of close buttons ad closes, page reloads , ad right was. trying figure out appropriately place "prevent default" code either works before ad can show first time, or in case shown here... not @ all.

i realize using both jquery , js new , piecing code find it.

<script type="text/javascript"> $(".close").click(function(){ $("#summercampad").addclass("hidden"); onload.preventdefault();  });  window.onload = function () { document.getelementbyid("summercampad").classname = document.getelementbyid("summercampad").classname.replace   ( /(?:^|\s)hidden(?!\s)/g , '' ) };  </script> <div id="summercampad" class="hidden" > <div id="popupcontainer">     <div id="closepopup">         <a href="#" class="close"><img src="https://cdn6.bigcommerce.com/s-p98jqjm/product_images/uploaded_images/closebtn.png?t=1429039040" alt="close"/>     </a>     </div>     <div id="pic">         <img src="https://cdn6.bigcommerce.com/s-p98jqjm/product_images/uploaded_images/popuppic.jpg?t=1429038436" alt="basketball"/>     </div>     <div id="popuptext">         2015 summer camps<br>          boys &amp; girls • skill level<br>         <span>space limited - sign now!</span>     </div>     <div id="registerbtn" >         <a href="/camp-registration/">         <img src="https://cdn6.bigcommerce.com/s-p98jqjm/product_images/uploaded_images/registerbtn.png?t=1429039883"/>         </a>     </div>     <div id="nobtn">         <a href="#" class="close">         <img src="https://cdn6.bigcommerce.com/s-p98jqjm/product_images/uploaded_images/nothanksbtn.png?t=1429039884"/>         </a>     </div> </div> 

just add @ end : return false. prevent action perform want.

<script type="text/javascript"> $(".close").click(function(){     $("#summercampad").addclass("hidden");     return false; }); ... 

everything describe on other post : return false jquery click event

also

you need first declare html, jquery. now, $(".close") return null because html has not been parsed browser.

the best solution use $(document).ready()

your code should :

<script type="text/javascript">     $(document).ready(function () {         $(".close").click(function(){         $("#summercampad").addclass("hidden");         return false;     }); }); ... 

this way first html loaded jquery bind click event on existing markup ;).


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -