Category : JQuery

Get duplicate id’s in DOM using jQuery

Get duplicate id’s in DOM using jQuery // Warning Duplicate IDs $(‘[id]’).each(function(){ var ids = $(‘[id=”‘'”]’); if(ids.length > 1 && ids[0] == this) console.warn(‘Multiple IDs #’; });

Create a new rule for jquery.validator plugin

Create a new simple rule for jquery.validator plugin by doing something like this: jQuery.validator.addMethod(“greaterThanZero”, function(value, element) { return this.optional(element) || (parseFloat(value) > 0); }, “* Amount must be greater than zero”); Apply the new rule: $(‘validatorElement’).validate({ rules : { amount : { greaterThanZero : true } } });

Bookmark Script using jQuery

Here is a bookmark script using jQuery: <script language=”javascript” type=”text/javascript”> $(document).ready(function(){ $(“a.jQueryBookmark”).click(function(e){ e.preventDefault(); // this will prevent the anchor tag from going the user off to the link var bookmarkUrl = this.href; var bookmarkTitle = this.title; if (window.sidebar) { // For Mozilla Firefox Bookmark window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,””); } else if( window.external || document.all) { // For

Read More →

jQuery – preview button in new tab(window), target blank

jQuery – preview button in new tab(window), target blank <!– DOCTYPE –> <html> <head> <title>jQuery – preview button in new tab(window), target blank</title> <script src=””></script> </head> <body> <form id=”my-form” method=”POST” action=””> <input type=”submit” name=”submit” id=”submit” value=”OK”> <input type=”submit” name=”preview” id=”preview” value=”Preview”> </form> <script type=”text/javascript”> jQuery(document).ready(function() { jQuery(‘#preview’).click(function(){ jQuery(‘#my-form’).attr(‘target’, ‘_blank’); }); jQuery(‘#submit’).click(function(){ jQuery(‘#my-form’).removeAttr(‘target’); }); }); </script>

Read More →

Verify if a checkbox is selected (checked) with jQuery

Verify if a checkbox is selected (checked) with jQuery <form name=”my_form_name” id=”my_check_box”> <input type=”checkbox” name=”my_check_box” id=”my_check_box”> </form> <script type=”text/javascript”> jQuery(document).ready(function() { jQuery(‘#my_check_box’).click(function () { if (jQuery(this).is(‘:checked’)) { alert(‘My checkbox is checked’); } } } </script>

jQuery – prevent form submitting

Method to prevent form submitting using jQuery: <form method=”post” name=”my_form_name” id=”my_form_id”> <input type=”submit” value=”Go”> </form> <script type=”text/javascript”> jQuery(‘#my_form_id’).submit(function() { alert(‘Prevent form submitting.’); return false; }); </script>

JQuery – Ajax request with jQuery

Perform an Ajax request with jQuery: jQuery.ajax({ url: ‘url_of_ajax_request’, success: function(response) { alert(‘Response from url is: ‘ + response) } }); Source: JQuery Ajax