Jquery Image Toggle Script Tutorial

This tutorial will show you just how easy it is to create a fancy toggle effect in jquery. to see the demo in action click here

Step1 – create you page

So firstly you will need jquery attached to you page, if you dont already have a copy, you can download it from google. Once you have this, simply upload it to you directory and call it into your page like so:

<script type="text/javascript" src="resources/js/jquery.js"></script>

Step 2 – the page elements

We want our toggle function to effect an element on the page, so just to keep things simple I have created a couple of divs on the page, one to hold our toggling image and another to hold some text, which we will show and hide depending on the toggle state.

<div id="box">
click to see the box toggle =><img id="show" src="images/add.png"/>
<div class="row1">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed porta, neque pulvinar placerat suscipit,   erat   mi commodo enim, non placerat neque nisi eu purus.       Nam nisl. Nullam in ipsum quis odio euismod tincidunt.    Nunc in tellus vitae lacus sodales volutpat.
</div>
</div>

Step 3 – writting the code

To start things off we want to activate our code as soon as the page loads, which means we need an onload function. Inside of this we want to hide the box containing the text, so we select the div class “row1″ and hide it.

$( function() {
  $('.row1').hide();
} );

Now comes the clever part. To begin, we select the id of the image and set a toggle function to initiate when we click the element. when this is triggered we set several events in motion.

$('#show').toggle(function(){
  $('.row1').slideDown("slow");
  $(this).attr("src","images/cancel.png" );
});

firstly to display the text we were hiding we use slideDown() which gives us a pretty effect. Then we declare a change in the src location of the toggle image, $(this) is used an awful lot in jquery, its simply a reference to the element that we are acting upon, in this case <div id=”show”> as you can see above. the attr() describes the attribute of the element, which we want to change from images/add.png to images/cancel.png. Then we simply close the funciton.

Step 4 – The really clever part

executing the above code will, work in displaying the text and switching the image, but what about when we want to switch it back? Well this is where the toggle function excels. Unlike a click() function which will execute its function the same way everytime, toggle() alternates between 2 function in effect toggling(what a suprise). So immediatly after the first function we use a ‘,‘ to chain another function, which looks like so.

,function(){
  $('.row1').slideUp("slow");
  $(this).attr("src", "images/add.png" );
}

Above we are simply reversing the intial function , by sliding the text back up and switching the image back to the original.

Step 5 – Putting it all together

Put it together and your code should look someting like this.

$(function(){
  $('.row1').hide();
  $('#show').toggle(function(){
    $('.row1').slideDown("slow");
    $(this).attr("src","images/cancel.png" );
  },function(){
    $('.row1').slideUp("slow");
    $(this).attr("src", "images/add.png" );
  });
});

In just a few short lines of code jquery has enabled us to pull off some pretty sweet moves. To see a working demo of the code click here. happy coding.


Improving Programming Productivity

I recently read a very interesting artical written by Matthew More describing how to improve a programmers productivity. With so many distractions on the web it seems increasingly difficult to stay focused in this ever evolving environment. Countless times I’ve found myself sidetracked from doing the most straight forward task and im sure im not the only one. Be it stumble, google reader, facebook, or youtube, these are all day to day distractions which can effect your productivity, and accumulate to time wasted if your in a work environment. So here are my suggestions:

1 – Segment Your Day

Try to break your day down, give yourself a short timeout in the morning then again at lunch, let this be your time when you can disengage from your work and get your daily feeds update your status etc.

2 – IM Only When You Need To

One of the single biggest distractions in any form of workplace is instance messaging. The best rule of thumb is to EM(email) before you IM, Having 1 less distraction can make a world of difference espcecially when your in the middle of a problem and you completely loose your train of thought because someone needed to ask you a question they already new the answer to.

3 – Observe the 6 P’s

The best Lesson I ever learned about planning was the 6 P’s - Proper Planning Prevents Piss Poor Performance. By planning something you are able to think it through and see issues as they arise. You can do this in a lot of different ways but you just cant beat the good old pen and paper.

4 – Know When To Stop

Web programming, like computer programming can be extremely addictive, as we all know, there is a fine line between work and addiction. working for 8-10 hours a day can take its toll, so its important that when your away from your projects ie on holiday or even a weekend break you leave your computer at your desk, more than just allowing you to switch off, it will keep your game up when you do return to your work.

5 – Organise Your Desktop

By keeping your tools accessable to you and having a clean environment in which to work you will find your productivity will increase ten fold. Be it your Dock or your programs, having better arrangement of your applications greatly improves your work effectivness overall because you only deal with the tools you need, having WOW running in the backgroud will not achieve the same turnover of workflow.

I hope these tips will help improve your work effectoveness and get more done in less time. If you have any tips and suggestions of your own, please feel free to discuss them.


Ajax in Jquery – A Simple Tutorial

Ajax has been appearing all over the web just lately, but the question most people find themselves asking is – what is it?

Ajax stands for Asynchronous Javascript And Xml, sounds scary but its really not. The main concept behind ajax is to reduce the number of times you have to jump from one page to another in order to find information. so basically, if you are on a page and the information you need is on another, ajax allows you to call that information to you, all from the comfort of your current URL.

Now this is a pretty neat party tick, so its no wonder that everyone has begun adopting it. And don’t get me wrong, this is a very cool trick, and if you use it wisely it can vastly improve the usability of your site. Ajax becomes even cooler when you use it with the jquery framework, mainly because it is so simple. All you need is this;

  $.get("test.php", { name: "fred", date: "2pm" },
  function(data){
    alert("Data Loaded: " + data);
  }
);

Lets break it down,  First off this is a GET request hence the $.get() and all were doing here is giving $.get() 3 arguments. The first is the URL which you want the ajax to send your info to. The second is the url parameters you wish to send, so this would look like “http://www.test.php?name=fred&date=2pm”, which makes sense, given were using a GET request. Finally the third argument is the function which calls back the result of the page, (data) being the result and then we are simply echoing the result with a basic javascript alert funciton. To see a working example of this script in action, why not check out my contact page.

If your now itching to try it for yourself ajax dady offers some scripts to try out, or for a more indepth introductory tutorial why not check out Web Designer Wall and give it a go youreslf. However if your not familiar with jquery principles you should definatly take a look at 15 days of jquery. happy coding.


Why Open Source Is The Future

As the web has evolved its unlocked new possibilities in the way software and applications are written. To some this is a bad thing, but to most it marks a turning point in the way we share, develop and innovate our ideas.

The progression of open source solutions came form the simple idea of sharing information. Although it has become something of a buzz word just lately Open source has been around a long time, as any Linux user will tell you. There are a multitude of reasons why open source has been so successfull, Satheesh Babu puts together a compelling argument why open source is about more than just being free, there are obvious gains to be had in the cost effective nature of open source applications and software, but more than this, its about solutions that work. PHP is a perfect example of a very successful web language which has been adopted by many, myself included. You only need to look at the latest and greatest online applications and social netowrking environments which are all rolling on it, wordpress and facebook have all been widely successful, and I cant help but feel this in part is due to the open source nature of the projects and the foundations they are built upon.

One of the key reasons open source projects have been so successful is the support that they offer, online wikis abd journals mean its easier than ever to find help and get issues resolved, the same with bugs and issue, and features which may be added over time, open source projects are like children, they grow because many people contribute to their experimce and after time they become wise and know what is to be expected.

Now I dont wish to upset, but in my mind the future seems pretty set, open source is the way things are going, and with ASP.net slowly dying out, developer and designers alike are embracing this brave new world. The real success story here isnt the improved support, or the fact that its free, its about the pride and passion that gets put into open source projects, where money is no longer the driving force, its something far more rewarding, that of the pride and peace of mind the contributers have towards the projects, to know that they have created something that helps and actually makes a difference, and may just makes peoples lives that little bit easier, and thats something i dont think anyone can put a price on.


Network Navigation, Riding on googles chrome

With google announcing, somewhat accidentally, the launch of its new browser google chrome, it has left a lot of web designers and developers pondering the question, do we really need another browser? After reading the cartoon google do give some good justification for why thay have chosen to create a new browser. The ability to use multi tab browsing effectively without the hinderence of slowed browser speed is one strong pro to the new system and i cant help but feel that maybe google are been very tacticle in delivering the product to PC users first. If google can break the deadlock of IE6 by getting PC uses to give up the ghost and adopt a newer, better browser, which offers a better user experience, that would be no mean feat.

But the question bothering me and indeed many others is if google felt there was nothing better out there, and thus they would have to create there own totally unique browser, why did they take mozilla’s existing code base to build upon? Further more, they claim that chrome is completely open source, so anyone can work to improve it… isnt firefox already that exact being? an open source project from start to finnish which has been a marvel for developers and web designers alike who have an array of toosl at their disposal, not to mention the end user experince.

The problem i truely have with google chrome is that its doing something, that was already done, then taking the credit. If google wanted to make a smart move they should have simply put a link to firefox on the google search page, and contributed to the open source code base which is already available. But then technically it wouldnt have been ‘all credit reserved’ google combined with the fact google would need to share its information with outsiders, heaven forbid!

Now I don’t want to put people off, my advice would be try it out, and if you cant do that (mac & linux users) try reading a couple of reviews techzilo and DevCentral have a good introductions to the topic with some screens of what to expect. But remember this, everytime a new browser walks onto the web thats another set of testing and as much as I hate to suggest it another set of hacks to make things work all over. So perhaps the only way to move forward is to step back and assess what we have, and what we need, then maybe we get the compatability sorted once and for all.


The Integration of Networks

‘The problem with communication… is the illusion that it has been accomplished.’

(George Bernard Shaw)

One would assume that a netowrk, in effect a series of computers connected to one another, would offer only a limited number of functions. That one could share data, disribute information and so on. Yet with the rise of network non-places, there in lies an evolution, a cultural phenomenon which has slowly been encouraging a change in the way one would percieve the internet and indeed any network. The rise of concepts such as ‘myspace.com’ and ‘facebook.com’ have brought with them a trend, a fashion if you will. That an individual may feel so insecure in thier being that, it is found neccessary to describe, if not validate ones self in these non-places. The influence of being webwize has extended itself, through the need to be digital. To exists as flesh and bone, as well as inprint ones ‘digital representation’ in a non-place. One looses the concept and meaning of communication, in expressing a feeling, or statement in a public arena, for all to see. This may perhaps be for a greater sense of importance, as a statement of self. However it is percieved as both a new media and sociological development. It may well be that of a passing trend, or it may be the start of something bigger. The need to fulfil ones wants and desires as though it were a exhibition to others. This concept may be foriegn to some, but think about it the next time you are reading a posting, or a comment in a public space, ask yourslef, why is this here?, why would one choose such an open arena to display a message or pose a question? As a concept it intruges me, Your views may be posted below in the irony of this illusion.