October 2008 Entries

Over the last couple of weeks I have been doing alot of work on a UI for client/matter conception application at my work involving javascript and the almight framework jQuery.  I have done a couple of posts in relation to jQuery before:

  1. Subtext Galleries, using jQuery for funk
  2. Adding a Javascript onClick event to a Radio Button List Item on DataBound
  3. Adding a Javascript onClick event to a Radio Button List Item on DataBound Pt 2

Also my 1st ever patch to subtext project uses jQuery.

After receiving countless emails about some of the above I thought I would post a couple of articles to outline how jQuery can make any developers life easier. This post is aimed at developers who haven't looked at jQuery before or people who want quick and easy examples. This is a very basic article and I will be going into more depth of the fQuery framework over the next couple of months.

How I am going to do this is show the javascript equivalent and the jQuery statement. Worst case is you walk away from reading this article knowing how to manipulate html elements.  If you're doing any manipulation to the dom it's always worth putting in validation to make sure the element exists.  Also note that the object that's being passed to the function is the elements Id.

This first part will compare the following:

  • Show/Hide an element
  • Toggle an element
  • Adding a css class to an element
  • Removing a css class from an element
  • Adding them together

 

show/hide an element - Javascript

function HideElement(object)
{
document.getElementById(object).style.display = 'none';
}

 

function ShowElement(object)
{
document.getElementById(object).style.display = 'block';
}

 

show/hide an element - jQuery

function HideElementjQuery(object)
{
$('#' + object).hide('fast');
}

Usage: <a href="#" onclick="javascript:HideElementjQuery('objectid');">Click Me</a>

function ShowElementjQuery(object)
{
$('#'+ object).show('fast');
}

Usage: <a href="#" onclick="javascript:ShowElementjQuery('objectid');">Click Me</a>

Thoughts on show/hide an element

While both are straight forward you'll notice that on the jQuery functions there is a "show('fast')".  jQuery gives the developer options to play with.  This can be the speed in which the object is hidden or shown, it can also be the effect you'd like to use when it appears and disappears. More information can be found UI/Effects - jQuery

Toggle an elements display property - Javascript

function toggleElement(object)
{
    if(document.getElementById(object).style.display != 'none')
        {
            document.getElementById(object).style.display = 'none';
        }else{
            document.getElementById(object).style.display = '';
        }
}

 

Toggle an elements display property - jQuery

function toggleElementjQuery(object)
{
$('#' + object).slideToggle('slow');
}

Usage: <a href="#" onclick="javascript:toggleElementjQuery('objectid');">Click Me</a>

Thoughts on toggle an elements display property

Straight away you see a reduction in code here. How simple is that. jQuery does the checking for you and dictates what state the object should be in.

Adding a css class to an element - Javascript

function addcssclass(object,cssclass)
{
document.getElementById(object).setAttribute("class",cssclass);//For FF
document.getElementById(object).setAttribute("className",cssclass);//For IE
}

Adding a css class to an element - jQuery

function addcssclassjQuery(object,cssclass)
{
$('#'+object).addClass(cssclass);
}

Usage: <a href="#" onclick="javascript:addcssclassjQuery('objectid','cssclass');">Click Me</a>

Thoughts on adding a class to an element

Again another reduction in code and jQuery takes away the worry of what browser the user is using and adds the class. Another flaw with the javascript code is that if the object had a css class by default then this is removed and the new one assigned.  jQuery adds to any css classes that may already be assigned to the object.  If we added a method to check for existing css classes and to add our new class then we'd run into may more lines of code that would have to be cross browser compatiable.

Removing a css class from an element - JavaScript

function removecssclass(object,cssclass)
{
document.getElementById(object).removeAttribute("class",cssclass);//For FF
document.getElementById(object).removeAttribute("className",cssclass);//For IE
}

Removing a css class from an element - jQuery

function removecssclassjQuery(object,cssclass)
{
$('#'+object).removeClass(cssclass);
}

Usage: <a href="#" onclick="javascript:removecssclassjQuery('objectid','cssclass');">Click Me</a>

Thoughts on removing a class from an element

As with adding a css class jQuery has taken away the worry of what browser the user is using.

Adding them together

This I am going to add a class while toggling the elements display property.

function ToggleAddClass(object,cssclass)
{
$('#'+ object).slideToggle('slow').addClass(cssclass);

}

Usage: <a href="#" onclick="javascript:ToggleAddClass('objectid','cssclass');">Click Me</a>

function ShowElementRemoveClass(object,cssclass)

{

$('#'+ object).show('fast').removeClass(cssclass);
}

Usage: <a href="#" onclick="javascript:ShowElementRemoveClass('objectid','cssclass');">Click Me</a>

Thoughts on adding them all together

As you can see from the above piece of code all that's needed is to add the effects needed to the object.

Final Thoughts

This post outlines the tip of the iceberg but as you can see one liners can do so much without having to worry about what browser is being used, or having to write long complicated functions.  Part will be looking at input Element Manipulation and validation.

Couple of days ago while checking out the Scottish resorts to see what the story was in relation to snow and nothing really stuck out.  Today is a different matter.

Taken from : Glencoe Mountains live feed - Click picture to enlarge

Glencoe 22 10 08

Taken from : Cairngorm Mountains live feed - Click picture to enlarge

Cairngorm Mountain 22 10 08

Taken from : Nevis Range live feed - Click picture to enlarge

Nevis Range 22 10 08

Lets hope that the snow keeps falling so that the resorts can open.  As always keep an eye on Visit Scotland for snow conditions.

Update

majorca 2008 It's been over 2 weeks since I last posed on my blog and the reasons are simple, holiday and real life.  Holiday to Majorca was fantastic,  unfortunately the weather wasn't that great but with old and new friends much fun was had.  Yes, being British, chucking it down with rain and we're all the in the pool playing volleyball.  I am not sure if it was holiday spirit or the vodka slushes but everyone was having a laugh.  I will be uploading all the holiday pictures to Flickr over the next couple of days.  If you'd like to see more of Alcudia and alot of people out enjoying themselves then keep an eye on my Flickr Gallery menu on the right or my main Flickr page.

Alcudia can be found North of the island of Majorca and has alot to offer. Transfer from the airport was about 50 minutes and if you're lucky enough to get a transfer during the day then alot of scenery to see on route.  We stayed at a massive complex called the Bellevue. The complex consisted of over 1400 apartments, several pools, bars, shops, a harbour and an entertainment venue.  Being all inclusive the variety of food each night was impressive and tasty.  The breakfast, well for something that tastes normal(bacon, sausages) then I would recommend going out of the complex.  If you're a group of friends, a couple, or a family I would thoroughly recommend the Bellevue.  People are friendly, regularly patrolled by the local authorities and you get a feeling of being safe.  Outside the complex and further into town is a different matter.  Unfortunately 2 of my friends had an encounter with a group of guys getting out of a car trying to mug them.  They weren't successful but things like this do happen everywhere.  Probably not a good idea to walk home a little drunk and taking a de-tour off the main road. Safety in numbers. For more about the resort of Alcudia check out the Alcudia Guide.

What's this white stuff falling?

Yeap, it's already that time of year when we put the boards into Summits for a service. This year will see my p1's being sold on Ebay as I am looking into Flow bindings as I am getting feed up of my mate Sammy being half way down the mountain by the time I all hooked in.  If you'd like to make a sensible offer before I put them on ebay get in touch.  Most resorts are showing to have a little snow, not enough for any riding yet but we can only hope that the season will kick off early this year.

As usual keep an eye on visit Scotland for all the usual updates.  I believe the last road trip we had last season took Baz and I up to Nevis to find they weren't opening due to high winds.  This resulted in going to Glencoe where we had one of the best days boarding in a Scottish resort, more on our Nevis and Glencoe adventure.

Further Update

I will be carrying on my blog skinning articles soon as everything has got back to normal.  I have also been working on an inline post editing feature for my blog which is nearly complete. It's just been busy busy busy. I am also looking for advertisers for another project of mine www.podcastkicks.com if you'd like to put an ad on the site then please get in touch.  A big drive for the site will be happening over the next couple of weeks.  If you're a podcaster or a fan of several podcasts that you'd like to share with the world with a possibility of earning some money check it out.

I'm test driven