Tag : jquery-2

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="'+this.id+'"]');
if(ids.length > 1 && ids[0] == this)
console.warn('Multiple IDs #'+this.id);
});

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 IE Favorite
window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
} else if(window.opera) { // For Opera Browsers
$("a.jQueryBookmark").attr("href",bookmarkUrl);
$("a.jQueryBookmark").attr("title",bookmarkTitle);
$("a.jQueryBookmark").attr("rel","sidebar");
} else { // for other browsers which does not support
alert('Your browser does not support this bookmark action');
return false;
}
});
});
</script>

HTML code:


<a href="http://www.g31zone.com" title="g31zone.com" class="jQueryBookmark">G31zone</a>

Read entire article at: http://www.developersnippets.com/2009/05/10/simple-bookmark-script-using-jquery/

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="http://code.jquery.com/jquery-latest.js"></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>
</body>
</html>

jQuery Ajax – send all fields data by “POST”

jQuery Ajax – send all fields data by “POST”


jQuery.ajax({
data: jQuery('#formId').serialize(),
// etc..
}

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