Category Archive: Code

Use jQuery to highlight input boxes on focus.

// Make focused fields stand out.
$('input[type="text"]').addClass("idleField");
$('input[type="text"]').focus(function() {
$(this).removeClass("idleField").addClass("focusField");
if (this.value == this.defaultValue){
this.value = '';
}
if(this.value != this.defaultValue){
this.select();
}
});
$('input[type="text"]').blur(function() {
$(this).removeClass("focusField").addClass("idleField");
if ($.trim(this.value == '')){
this.value = (this.defaultValue ? this.defaultValue : '');
}

Permanent link to this article: http://www.lukemacneil.com/2010/04/23/use-jquery-to-highlight-input-boxes-on-focus/

Strip bad characters from textarea as you type with jquery.

<script language='javascript'>
/*
* This function does some client side input validation. Automatically removing
* Bad characters before they cause a problem. Additional validation is done on the
* server.

* We're also hacking the onpaste event.
*/
$(document).ready(function(){
//set keyup event
$("body").keyup(function(){
if ($("#body").val().match(/[^a-zA-Z0-9 n.,?]/g)) {
$("#body").val($("#body").val().replace(/[^a-zA-Z0-9 n.,?]/g, ''));
}//end if
}); // end keyup

$(document).bind('paste', function(e){
if ($("#body").val().match(/[^a-zA-Z0-9 n.,?]/g)) {
$("#body").val($("#body").val().replace(/[^a-zA-Z0-9 n.,?]/g, ''));
}//end if
})

});// end docready
</script>

Permanent link to this article: http://www.lukemacneil.com/2010/04/23/strip-bad-characters-from-textarea-as-you-type-with-jquery/

jQuery Confirmation on Class

Code to assign a jquery dialog to the .confirmClick class on any element.

$(".confirmClick").live("click", function(event) {
// Stop the default action from occuring.
var clickedButton=$(this);
event.preventDefault();
var targetUrl = $(this).attr("href");

// Set up the error message,
if ($(this).attr('title')) {
var question = 'Are you sure you want to ' + $(this).attr('title').toLowerCase() + '?';
} else {
var question = 'Are you sure you want to do this?';
}
//  copy it to a hidden element.
$("#dialogmsg").html(question);
// Initiate the dialog box
$("#dialog").dialog({
bgiframe: false,
resizable: false,
height:140,
modal: true,
overlay: {
backgroundColor: '#000',
opacity: 0.5
},
buttons: {
OK: function() {
$(this).dialog('destroy');
// Set behavior based on link or form.
if (targetUrl){
window.location.href = targetUrl;
}else{
//$(clickedButton).attr("name").submit();
$(clickedButton).closest("form").submit();
}

},
Cancel: function() {
$(this).dialog('close');
$(this).dialog('destroy');
return 1
}
}
});

});

Permanent link to this article: http://www.lukemacneil.com/2010/04/23/jquery-confirmation-on-class/

Simple backup script for VMWare Workstation.

There are more sophisticated ways of doing this, but here is a simple script I’m using to backup my virtual machines. It runs out of cron at 3am on Sundays. I also take snapshots of the VM before any major change.

#!/bin/bash

echo "BACKUP Virtual Machines Initiated"
echo "Suspending running VMs"
vmrun -T ws suspend "/datastore/Virtual Machines/Windows 7 x64/Windows 7 x64.vmx"
echo "Copying VMDKs"
rsync -az "/datastore/Virtual Machines/Windows 7 x64" "/backup/Virtual Machines"
echo "Restarting suspended VMs"
vmrun -T ws start "/datastore/Virtual Machines/Windows 7 x64/Windows 7 x64.vmx"
echo "Complete"

Permanent link to this article: http://www.lukemacneil.com/2009/12/10/simple-backup-script-for-vmware-workstation/

JQuery to make a lazy filter (like iTunes)

This code is not mine, I’d link out to the source, but I’ve forgotten it. If you wrote this, thank you.


$('tbody tr').hover(function(){
$(this).find('td').addClass('hovered');
}, function(){
$(this).find('td').removeClass('hovered');
});

//default each row to visible
$('tbody tr').addClass('visible');

$('#filter').keyup(function(event) {
//if esc is pressed or nothing is entered
if (event.keyCode == 27 || $(this).val() == '') {
//if esc is pressed we want to clear the value of search box
$(this).val('');

//we want each row to be visible because if nothing
//is entered then all rows are matched.
$('tbody tr').removeClass('visible').show().addClass('visible');
}
//if there is text, lets filter
else {
filter('tbody tr', $(this).val());
}
});

//filter results based on query
function filter(selector, query) {
query =   $.trim(query); //trim white space
query = query.replace(/ /gi, '|'); //add OR for regex query

$(selector).each(function() {
($(this).text().search(new RegExp(query, "i")) < 0) ? $(this).hide().removeClass('visible') : $(this).show().addClass('visible');
});
}

Permanent link to this article: http://www.lukemacneil.com/2009/11/06/jquery-to-make-a-lazy-filter-like-itunes/

Older posts «

» Newer posts