Code

Read boolean HTML attributes with jQuery

Friday, 1 January 2010 @ 13:02

After much testing, I have concluded that in order to use jQuery with boolean attributes (and have it work in the big four browsers), the following markup must be used: checked=”checked”.

While browsers except any of the following…

checked
checked=""
checked="true"
checked="1"
checked="checked"

…only the last option works with jQuery in all browsers.

Display 12, read/write 24 TimePicker jQuery plugin

Wednesday, 30 December 2009 @ 17:47

Jason Huck’s jQuery TimePicker plugin didn’t work for me as-is since it reads and writes in 12-hour format. While most people (strangely) prefer 12 hour format for interacting with, database time fields are 24 hour. I could have converted the format with scripting on the back-end but decided I would prefer to have the picker display 12-hour but read/write 24-hour. So, I took Jason’s great design and rewrote it to suite my needs.

Other changes I made were adding a colon between the hour and the minute select lists, adding a space between the minute and the am/pm list, and changed the hours and minutes arrays such that the hours no longer have leading zeros and the minutes are every five instead of every fifteen.

I have intergrated tomsalfield (issue 3) and jasonalanharris’ (issue 5) changes as well.

My version of the script can be found in the issue tracker on the Google Code project for the plugin. Here’s a screenshot of it in use next to a date field:

zebra striping across rowspans with jQuery

Saturday, 13 December 2008 @ 10:46

On one of my current projects, I am adding alternating shading (“zebra striping”) to rows in a table with javascript. This last week I encountered a scenario where I needed this to work on a table that had some cells that spanned multiple rows.

My current script could not handle this as it was using jQuery’s :odd selector and I now needed to stripe all of the spanned rows together instead of just every other one. The additional shading that I was applying when hovering over the row was also broken as it too needed to shade all of the spanned rows instead of just the one the mouse was over.

So, I enhanced my script to work with both scenarios. I hope it can be of use to someone.

// check for rowspans
rows = $('.datatable > tbody > tr:first > td:first').attr('rowspan');
var rows_count = rows * 1;
 
// if, shade spanned rows; else, shade for every other row
if(rows_count > 1) {
   // find the row group leader
   function rgl(who) {
      var current = who;
 
      for(i = 0; i < rows_count; i++) {
         if($(current).find('td:first').attr('rowspan') > 1) {
            var rgl = current;
            break;
         }
 
         current = $(current).prev('tr');
      }
 
      return rgl;
   }
 
   // row shading
   for(r = 1; r <= rows_count; r++) {
      var nth = (rows_count * 2) + 'n+' + (rows_count + r);
      $('.datatable > tbody > tr:nth-child(' + nth + ')').addClass('odd');
   }
 
   // row hover shading
   $('.datatable > tbody > tr').hover(
      function() {
         var who = rgl(this);
         for(i = 0; i < rows_count; i++) {
            $(who).find('td').toggleClass('hover');
            who = $(who).next('tr');
         }
      },
      function() {
         var who = rgl(this);
         for(i = 0; i < rows_count; i++) {
            $(who).find('td').toggleClass('hover');
            who = $(who).next('tr');
         }
      }
   );
} else {
   $('.datatable > tbody > tr:odd').addClass('odd');
}

Chainlove alerts

Sunday, 12 October 2008 @ 21:02

Any other cyclists out there? You a chainlove junkie too? After finding myself spending too much time refreshing and refreshing, I wrote the following script to watch the site for things I was interested in and send me a text message when they come up. I put it on my machine that’s always on and scheduled it to run every ten minutes with launchd. Hopefully it will save you a little time and sanity as well. (Note the wrapped lines.)

/* 
 * CHAINLOVE ALERTS
 * 
 * This script scrapes chainlove.com and sends an email when words in 
 * the "watch list" are included in the current product offering.
 */
 
// list of words to watch for
$watch_list = array('sunglasses', 'warmers', 'jersey', 'giordana');
 
// cache file (including path)
$cache_file = '/tmp/chainlove_alerts.cache';
 
// scrape page
$ch = curl_init() or die(curl_error()); 
curl_setopt($ch, CURLOPT_URL, "http://www.chainlove.com"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$page = curl_exec($ch) or die(curl_error()); 
curl_close($ch);
 
// pull out the product details
preg_match('/<h1 id="item_title">([^<]+)<\/h1>.*<h3 class="price"> ¬
   ([^<]+)<\/h3>.*<div id="regular_price">\n*\t*\s*([^<]+)<br \/> ¬
   \n\t*\s*([^<]+)<\/div>/sm', $page, $matches);
 
// compare current product against cache product... if same, exit
if(!file_exists($cache_file)) {
   file_put_contents($cache_file, trim($matches[1]));
} else {
   $cache_product = file_get_contents($cache_file);
 
   if($cache_product == trim($matches[1])) {
      exit();
   } else {
      file_put_contents($cache_file, trim($matches[1]));
   }
}
 
// search current product title with "watch list"
$send_mail = 0;
foreach($watch_list as $watch_item) {
   if(stripos($matches[1], $watch_item)) {
      $send_mail++;
   }
}
 
// send email
if($send_mail > 0) {
   $mail['to'] = '8015551212@txt.att.net';
   $mail['subject'] = trim($matches[1]);
   $mail['message'] = trim($matches[2]) . ' (' . trim($matches[3]) .
      ', ' . trim($matches[4]) . ')';
   $mail['from'] = 'Chainlove Alerts <user@example.com>';
   $mail['additional_headers'] = 'From: ' . $mail['from'];
   mail($mail['to'], $mail['subject'], $mail['message'], ¬
      $mail['additional_headers']);
}

pages

categories

events

syndication

random members

Utah PHP Users Group

recent posts

recent comments

recent job announcements

search

C7 Data Centerscolocated at C7 Data Centers
administered by Anavi Design