Used jQuery to add a header for a group of courses

We had a group of courses for Take our Daughter and Sons to Work Day added to Making Tracks that all used the abbreviation of the name in their title: TODSTWD. They didn’t spell out the event name anywhere. So I added a jQuery script to the header that prepended the listing with the full title.
I limited this to course listing pages and confined it to a date range so it stopped running after the event.

Here’s a screenshot of the course list summary with out the jQuery header (before):

And here’s a screenshot of the course list summary with the jQuery header (after):

It also runs on the indiv. course details page.

Here’s the code:

<?php
if(strtotime(date('Y-m-d')) <= strtotime(date('2011-4-29'))){ //display until 4-29-11
	$todstwd_script = <<<javascript
	/* ########## Add TODSWD JS ########## */
	Drupal.behaviors.testBehavior=function(context){
		//run only if there is a context var
		if(typeof context !== 'undefined'){
			/*
			Check the type of the context var and type. Process if context = [object HTMLDocument]
			Note: when context == [object HTMLDocument] then this is page load. You may want some script to run only on page load. Some not and some all the time.
			*/
			if(window.location.href.search(/course_view|course_list/)!==-1){
				$('.courseSummary h3 a').each(function(){
					if($(this).text().search(/TODSTWD/)!==-1){
						$(this).parent().parent().prepend(&#039;<h2 class="todstwd" style="color:#006600">Take Our Daughters and Sons To Work Day - 4-28-2011</h2>');
					}
				});

				$('#t_content h2').each(function(){
					if($(this).text().search(/TODSTWD/)!==-1){
						if(!$('#todstwd').length){
							$('#main').prepend('<h1 id="todstwd" style="color:#006600">Take Our Daughters and Sons To Work Day - 4-28-2011</h1>');
						}
					}
				});
			} //end if(window.location.href.search(/course_view|course_list/)!==-1){
		} //end if(typeof context !== 'undefined'){
	} //end Drupal.behaviors.testBehavior=function(context){
	/* ########## end Add JS ########## */
JAVASCRIPT;
	drupal_add_js($todstwd_script, 'inline');
}
?&gt;

Here’s a link:
Making Tracks | Organizational Development and Training ODT.

Leave a Comment