Create a jQuery Accordion – remain open after page load

March 30th, 2009

I am currently working on this site for a client: http://www.hornedpout.com/ – a very basic portfolio website for a designer/writer. I didn’t want to use the bulky code of the jQuery UI so I decided to use the smaller version, below:

$(".menu-header").click(function() {
        $(this).next().toggle('slow');
        return false;
}).next().hide();

The HTML would look something similar to:





That code will use jQuery’s toggle() function. When the user clicks the h2 tag it will close all elements until the next
tag. This snippet is very handy for creating a quick accordion effect, this can also be found on the jQuery’s documentation

Now we have the guts sorted out, but the problem is if you travel to another page within that category, it will close. I’m sure you want to check out the code that is going to do this.

Keep that open!

var matches;
     if( matches = (new String(document.location)).match(/?c=w+/) ) {
        $("a[href=" + matches[0] + "]").parents("ul").toggle();
     }

The code above is fairly straight forward. The first line checks to see if we already have a sub-menu open. It evaluates by looking at the current pages URL “?c=page_here”.

The latter part of the code. Now that we grabbed the current URL of the sub-menu, we can use this to find the parent menu which is the
ul and display it:

$("a[href=" + matches[0] + "]").parents("ul").toggle();

The quick fix to keep your accordion block open while traveling to different URLS. To achieve the URLS of “?c=page”. I am using this PHP code below save it as index.php, also create a header.php and a footer.php.

Give it a shot, hope that this can work for you, it did wonders for my project. Also thanks to Kent, for helping me. If it did work post some examples!

jQuery Timeout Function

January 22nd, 2009

jQuery does offer callbacks for most of its functions, but what if you want to delay/timeout a certain effect or function. Well with this article, this should help you achieve that.

I have been trying to search for a nice and reusable way to use jQuery to timeout effects or functions. We are familiar with using

setTimeout (function,timeout);

After putting in some data we can come up with something like this:

setTimeout (showBox(),2500);

Remember that the value 2500 is in MS (milliseconds), we equates to 2.5 seconds. When this function is called, it will execute 2500ms later. I was using this for one of my projects where I wanted two effects/functions not after, but during. I could have used very handy jQuery callback and load in a function, but it would have been executed after the first function is completed.

The solution for this? Quite simple, check out the jQuery $ function, which allows us to call jQuery as “$” the example is below.

(function($) {
    // Within this function $ is a reference to jQuery
    // I love this framework
})(jQuery);

Lets jump right into the belly of the beast and view the code to call our handy jQuery timeout function.

(function($){
	jQuery.fn.idle = function(time){
		var i = $(this);
		i.queue(function(){
			setTimeout(function(){
				i.dequeue();
			}, time);
		});
	};
})(jQuery);

This might look like a bit overload, but it is really simple, we just learned how to create the skeleton of the function, so without it we:

jQuery.fn.idle = function(time){
	var i = $(this);
	i.queue(function(){
		setTimeout(function(){
			i.dequeue();
		}, time);
	});
};

You see on the first line above we create the idle function: jQuery.fn.idle. We created the variable I to store $(this), which is going to be element that is running the $.idle(); function.

We then use the jQuery queue(); function whose documentation can be found on the jQuery website. Then we attach the $(this); variable we created called: i to it, and create the setTimeout(), which then uses dequeue(); with our variable connected to it.

The last part, where we create the time parameter for the function, so we call the following code and:

$.idle(2500);

time gets replaced with the user input of 2500ms to the function. Say if you want to call a function you can use it in this context instead of the messy old fashion setTimeout(); function.

$('.myelement').fadeIn().idle(2000).fadeOut('slow');

The above simple reads: fade in my element, then wait 200ms or 2 seconds then slowly fade out the same element. BOOM! There it is, the quick n’ dirty method to timing functions out using jQuery.

jQuery Search And Highlight

December 20th, 2008

I was sitting in class and wanted to come up with some simple way to add a inline same page ajax search on a website. I came across the jQuery selector :contains(text), check out the documentation for this. Now the to the final product, lets check out the demo. We are going to have our main components for this:

  • The form (1 input and submit)
  • The :contains selector
  • The fadeIn() effect
  • The addClass()

The approach for the code is going to be like

  • User will enter term in form
  • Save data to a variable
  • Place variable in to :contains selector
  • Fade in the highlight class (using fadeIn() and addClass())

Read the rest of this entry »

Busy…Busy

December 14th, 2008

Hey everyone! I have been extremely busy with the past couple of weeks due to the end of the semester coming and trying to get my finals and projects out of the way. Just about done with my ASP shopping cart application and my usability project. I am just about done with the fall semester, so expect some posts here soon in a week. I also meant to post my iGoogle theme I created, its nothing crazy, but thought I would make some fun of it. Try it out!iGoogle Theme - Summer Landscape

If you have any ideas or requests that you would like to see, please let me know, drop an e-mail or comment.

Finally updated my WP and I love the new interface so far, they did a great job on it.  Install went smooth as always.

I am currently working on a same page ajax search and highlight script using jQuery, it is almost done, so stay tuned. I am also going to make some tweaks to the layout, I need more color on this temporary one, you will probably be seeing some changes over the next couple of weeks.

Take care! Cheers.

Ryan

Combining Selectables And Draggables Using jQuery UI

November 23rd, 2008

There are two powerful and fun elements that the jQuery UI provides. When both of them work together you will be able to come up with some creative web application uses for them. The two I am going to be covering are:

I am sure you all want to see what the final product will look like, check it out.

Read the rest of this entry »