In: jQuery, Projects
Free Object Literal update
I’ve been building a lot of JavaScript in Object Literal lately and I thought it was about time I updated my core functions in that style.
I’ve merged two core functions into one object with this update, Title Hide and Input Clear are now one. I find I use them in similar situations so it made sense to bring them together in this way. I’ve also updated the jQuery to work better with release 1.6.1. Essentially nothing has changed with the functions apart from the syntax update. But what this change does facilitate is a quick and easy method to avoid conflicts. If you already have a ‘core’ object in your app, simply change any instances of ‘core’ to your object name and the function calls remain unchanged and working.
There are two ways for you to download the code
1. Copy and paste the code listed below.
2. Download a zip containing the source and production files.
/* http://www.designergamergeek.com/jquery-projects/
Version: 1.1 - 2011-05-31
License: none (public domain)
*/
// Core object
var core = {
storeTitle : "",
checkVal : ""
}
//Title Hide
$('.titleHide').live('hover', function(e) {
//Store the title for later
core.storeTitle = $(this).attr('title');
//Clear title
$(this).attr('title', '');
}, function(e) {
$(this).attr('title', core.storeTitle);
});
//Multi Purpose Input Clear
$(document).delegate('.input','focus',function(e){
//Get current (default if first time) value of input
core.checkVal = $(this).val();
core.storeTitle = $(this).attr('title');
//If the inputs title is empty, fill it with the default value
if (core.storeTitle == '' || core.storeTitle == null || core.storeTitle == undefined){
$(this).attr('title', core.checkVal);
}
//Create user input value
core.storeTitle = $(this).attr('title');
//If default value, clear it
if(core.checkVal == core.storeTitle){
$(this).val('');
}
}).delegate('.input','blur', function(e){
//Load variables
core.checkVal = $(this).val();
core.storeTitle = $(this).attr('title');
//If no user entry, fill with default title
if(core.checkVal == ''){
$(this).val(core.storeTitle);
}
});
I’ve been trying to find some more useful functions to add to the core object, but I’m being careful to only add functions that have high usage. The last thing I want to unnecessarily bloat the code. I do plan to post some other bits and pieces though so I’ll keep you posted.
Posted: Wednesday, July 6th, 2011
Tags: download, free, JavaScript, jquery, object-literal, update
Gamers Tees on Jumperlumps.com
No Responses