Useful jQuery Code Snippets

By using these code snippets you can save your pretty valuable time when developing user interfaces. Just copy the jquery code snippets and make your website with pretty nice effects

Scroll to Top link

This small piece of code is used to navigate the user back to top of the page when they are at the bottom of the page.

$(document).ready(function(){
$("a.scrolltop").click(function() {
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
});
});

You need to create a html link with class  scrolltop . That’s it

How to Reset / Clear the Form Values

This code will clear the form data after user submiting the form via ajax


$(document).ready(function(){
$('#formID')[0].reset();
});

formID  is the ID of the form.

How to Prevent Users from Submitting the Form Twice

This below code will work perfect even on forms that have multiple submit buttons


$(document).ready(function(){
var $myForm = $("#formID");
$myForm.submit(function(){
$myForm.submit(function(){
return false;
});
});
});

Zebra Table Design using jQuery

Zebra table is very famous and it is easy to differentiate alternate rows in a table. I hope this trick will improve the readability of the entire table. We need to write css code for class  .zebra  for background color

$(document).ready(function(){
$("table tr:even").addClass('zebra');
});

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *