Created
January 19, 2014 18:28
-
-
Save josesayago/8508903 to your computer and use it in GitHub Desktop.
Code snippets for "jQuery Tricks for Dummies" article, published on http://en.8elite.com/2012/09/13/jquery-tricks-for-dummies/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function(){ | |
// First we look for each image inside the div we want | |
jQuery('div.new-doodles img').each(function(){ | |
// Then we take only those with the file extension we want | |
if( jQuery(this).attr('src').match(/\.(jpg)/) ) { | |
// And then add them our CSS class | |
jQuery(this).addClass('my-awesome-class'); | |
} | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class='new-doodles'> | |
<img src="http://www.google.com/logos/2012/macedonia-independence-day-12-hp.jpg" id="image1" /> | |
<div class='image1'></div> | |
<img src="http://www.google.com/logos/2012/Teachers_Day_Alt-2012-hp.jpg" id="image2" /> | |
<div class='image2'></div> | |
<img src="http://www.google.com/logos/2012/teachers12-hp.jpg" id="image3" /> | |
<div class='image3'></div> | |
</div> | |
<div class='old-doodles'> | |
<img src="http://www.google.com/logos/2012/al_biruni-2012-hp.jpg" id="image4" /> | |
<img src="http://www.google.com/logos/2012/First_Day_Of_School_Isreal-2012-hp.jpg" id="image5" /> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form id='myform' method='POST' action='#'> | |
<input type='text' name='quantity' value='0' class='qty' /> | |
<input type='button' value='+' class='qtyplus' field='quantity' /> | |
<input type='button' value='-' class='qtyminus' field='quantity' /> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id='feed'> | |
<div class='content'> | |
<p>Hey visite us at http://www.laelitenetwork.com</p> | |
</div> | |
<div class='content'> | |
<p>Check out http://8elite.com and follow us</p> | |
</div> | |
<div class='nolink'> | |
<p>Look ma! no links http://en.8elite.com</p> | |
</div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function(){ | |
// This button will increment the value | |
$('.qtyplus').click(function(e){ | |
// Stop acting like a button | |
e.preventDefault(); | |
// Get the field name | |
fieldName = $(this).attr('field'); | |
// Get its current value | |
var currentVal = parseInt($('input[name='+fieldName+']').val()); | |
// If is not undefined | |
if (!isNaN(currentVal)) { | |
// Increment | |
$('input[name='+fieldName+']').val(currentVal + 1); | |
} else { | |
// Otherwise put a 0 there | |
$('input[name='+fieldName+']').val(0); | |
} | |
}); | |
// This button will decrement the value till 0 | |
$(".qtyminus").click(function(e) { | |
// Stop acting like a button | |
e.preventDefault(); | |
// Get the field name | |
fieldName = $(this).attr('field'); | |
// Get its current value | |
var currentVal = parseInt($('input[name='+fieldName+']').val()); | |
// If it isn't undefined or its greater than 0 | |
if (!isNaN(currentVal) && currentVal > 0) { | |
// Decrement one | |
$('input[name='+fieldName+']').val(currentVal - 1); | |
} else { | |
// Otherwise put a 0 there | |
$('input[name='+fieldName+']').val(0); | |
} | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function(){ | |
// Check the main container is ready | |
$('#feed').ready(function(){ | |
// Get each div | |
$('.content').each(function(){ | |
// Get the content | |
var str = $(this).html(); | |
// Set the regex string | |
var regex = /(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)/ig | |
// Replace plain text links by hyperlinks | |
var replaced_text = str.replace(regex, "<a href='$1' target='_blank'>$1</a>"); | |
// Echo link | |
$(this).html(replaced_text); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment