Skip to content

Instantly share code, notes, and snippets.

@josesayago
Created January 19, 2014 18:28
Show Gist options
  • Save josesayago/8508903 to your computer and use it in GitHub Desktop.
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/
<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>
<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>
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);
}
});
});
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