
event.preventDefault
This is a jQuery event that will prevent an action from happening. The code below says that:
1.) When the DOM is ready
2.) Every 'a' element that is clicked
3.) Create a random number between 0 - 5
4.) Test the number
5.) If the random number is equal to 5
6.) Show what number it was and go to the link provided
8.) Otherwise
9.) Dont let anyone use the anchor action
Snippet options
Download: Download snippet as eventpreventdefault.js.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!
$(document).ready(function() { $("a").click(function(event) { var i = Math.floor(Math.random() * 6); alert(i); if (i == 5) { alert("i must not be 5, its actually: " + i); } else { event.preventDefault(); } }); });
Create a free my code stock.com account now.
my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!
Find out more and register nowYou can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.