Posts Tagged ‘javascript’
jQuery Checkbox Replacement
Posted by: Steve Johnstone in Web Development on February 3rd, 2010
Ordinary checkboxes on the web are a bit boring. I’ve seen a few replacement scripts, but I thought that it would be pretty easy to write my own.
function replaceCheckbox(name, on, off) { $(name+':not(.checkbox-replaced)').each(function (){ var image = ($(this).is(':checked') ? on : off); var imageElement = $('<img style="cursor:pointer;" src="'+image +'" onclick="$(this).next().click();"/>'); $(this).before(imageElement).hide() .addClass('checkbox-replaced'); imageElement.click(function (){ $(this).attr('src',(this.src.indexOf(off) > 0 ? on : off)); }); }); }
It seems to do the job fairly nicely, so I’m quite happy. It does require the use of jQuery. To use it simply go:
replaceCheckbox('input[type=checkbox]:visible','check_on.png','check_off.png');
Where Did My Event Go?
Posted by: Steve Johnstone in Web Development on September 16th, 2009
For the past day I’ve been struggling with a bit of Javascript. Like most problems involving browsers, it worked perfectly in Firefox (Whoo!) but died a horrible and confusing death in Internet Explorer (Boo!).
The code was using jQuery to clone a selection of rows in a table and append them to the same table. This worked fine, but one of the rows had an autocompleting field, which refused to work the second time someone did a search. This only applied to the duplicated rows, and only on the second search. The first time worked perfectly, no matter how many rows had been duplicated.
The culpret turned out to be a div that was wrapped around the added rows, in order to manipulate the new rows as one jQuery object. It turns out that if the DOM is not perfectly formed in IE, any future manipulation of the DOM will result in events being stripped.
Increase JavaScript Responsiveness with setTimeout
Posted by: Steve Johnstone in Web Development on March 19th, 2009
My current project requires moving an item from one list to another, and then verifying the contents of the second list against the database. In order to do this, I used the jQuery append function, and then called another function which executed a getJSON request.
Because the speed of the request was so slow, it ended up delaying the move of the item. This made the UI feel unresponsive. In order to fix this, I changed the call to the function to a setTimeout with a delay of 0. This meant that the UI could go on and do what it wanted to, while the JSON request went off and did it’s thing.
Browser Caching
Posted by: Steve Johnstone in Web Development on March 19th, 2009
As any web developer will know, caching is something that is both extremly good, and annoying in equal amounts. It’s good because it reduces load on the server and speeds up response times, but bad when you want to see your changes in a timely manner. Fortunatly there are easy ways to get around the annoying cache while developing sites.
First of all, you can disable your cache completly. In Internet Explorer this is done through the options menu, and in Firefox it’s done through ‘about:config’. This is the most extreme method, and something I don’t really recommend.
My prefered method is to use the Web Development Toolbar in Firefox, that comes with it’s own disable cache mode. However, sometimes even that is a bit extreme. A much simpler method (and one that works in all browsers) is to open up your css or js file in another tab, and simply refresh that tab.