Posts Tagged ‘html’

Selecting all checkboxes in a group with Mootools, one-liner

Sunday, August 23rd, 2009
Fairly common..?

Fairly common..?

How many times have you had to do one of these things? A group of items, each with a checkbox. You want a button/checkbox to select all the checkboxes within that group.

I ran across this again today, and making the javascript to do the work I was once again reminded why I love Mootools so much:

With just three lines of code (1 line to do the actual checking/unchecking), I had this up and running. It couldn’t have taken longer than 30 seconds to write. I love quick snippets ;-)

Obviously, this wouldn’t be hard to do in jQuery or any other JS framework, but I love how elegant and intuitive Mootools does it. I shudder when I think of how many times I’ve done things similar to this in plain old Javascript.

Anyway, there’s a demo page available, and here’s the code I ended up with, for anyone interested:

$$('li.head input[type=checkbox]').addEvent('click', function() {
this.getParent('ul').getElements('input[type=checkbox]').setProperty('checked', this.checked);
});

String literals and lazy programmers

Friday, June 19th, 2009

A lot of programmers are passionate about what they feel is the “right” way to do things. A common topic of discussion is code standards, especially questions such as “Do brackets go on a separate line, or at the end of a statement”. It’s understandable, because I see pros and cons in both. On other topics, I have a hard time understanding both sides – such as whether to use tabs or spaces for indentation (I only ever use tabs). What we can all agree on however is what I started with: programmers are passionate about “the right way” to do things.

“String literals’

Heh, that was clever. See how I used both ” and ‘ in the heading there?

PHP is a funny language in a lot of ways. One of the things it does, along with some other languages such as Javascript, is allow you to specify strings using both double and single quotation marks (is that the right name for them?). In PHP, I generally just use what is most convenient at the time. There are differences between them (in PHP), which I’m sure most people are aware of already (double allows you to use variables directly in the string, using single you have to hop out and concatenate the variable with the . character. Some programmers apparently see this as an enormous amount of effort, and just use double quotes instead. Lazyness.

I generally find it easier to read code where variables are concatenated. If you want to rewrite the code to use values from an array instead of different variables, you either end up with the even uglier {$aArray['key']} style, or you end up rewriting the code to use concatenation anyway. Why not just do it “the right way” the first time you write the code?

The horror!

Even more horrible is using the single quotation marks for (X)HTML attributes. Most of the time people get this one right, but once in a while I stumble across pages that use the single quotation mark for HTML attributes – horrible stuff, even if it is “legal”. I was looking through the HTML for my blog today and fell off my chair when I saw this:

<ul class='xoxo blogroll'>

After peeking through the Wordpress code, I discovered that this was a someone obviously does not share my hatred for using single quotation marks in HTML ;-) .

Really though:

$output .= "$title_before$title_li$title_after\n\t<ul class='xoxo blogroll'>\n";
// Could easily be written:
$output .= "$title_before$title_li$title_after\n\t<ul class=\"xoxo blogroll\">\n";
// Or...
$output .= $title_before . $title_li . $title_after . "\n\t" . '<ul class="xoxo blogroll">' . "\n";

Personally, I prefer the last one, as I find it much easier to read. Plenty more grep’ing later, I think I’ve translated most of the occurrences (at least on the front page).

What are other peoples views on string literals? Am I just being a complete code nazi here?