Because sometimes I'm just fucking right...

Author: Mike (Page 4 of 5)

Javascript Magic w/Optgroups

A good 8 years ago or so, a co-worker of mine and I worked out how to use javascript with select objects to create a “to and from control” like you’d see in Outlook for selecting to, cc and bcc from your address book. Essentially we setup 2 selects with arrows in between. We populated to the two and then used javascript to move the options back and forth between the two. It’s a fairly straight forward process and the javascript is pretty simple once you strip it down. Since then I’ve always wanted to figure out how to do a to and from control using optgroups but have never taken the time to do so. Well, a recent project at work finally gave me the excuse to figure it out.

The requirements for this control were pretty straight forward. First, it must function in IE6, IE7 and Firefox. It should work by moving an option from one select to the other, while keeping the option within its defined optgroup. For instance, if Kansas is in the optgroup Midwest, when Kansas is moved to the other select, it needs to go in the Midwest optgroup. If the Midwest optgroup doesn’t exist then the optgroup needs to be created first. I also toyed with the idea of removing the optgroups once they were empty. I got it working but there are some odd idiosyncrasies to the way the hasChildNodes function works so I’m most likely not going to include it in my project. The final, and perhaps most important requirement, is that the tool needs to keep track of what items are added and removed from the right-hand side select. So, how do we do this?

To start, we pass in the ids of the three controls. The to select, the from select and the hidden that will be storing our values. Assuming they load appropriately, we should be good to go.

var DELETE_OPTGROUPS = false;

function optgroupMove(toID, fromID, addID)
{
  var to = document.getElementById(toID);
  var from = document.getElementById(fromID);
  var deletedOptions = new Array();
  var hidden = document.getElementById(addID);
  var index=0;

Next, we cycle over the from select. Any option that is currently selected will be moved to the “to” select. To do this, we retrieve the selected option, get its optgroup, create a new option, add it to the select and then assign the text and value to it. It may seem odd to do the text/value assign last but it was the only way to get the appendChild function to work in IE.

for(x=0; x<from.options.length; x++)
{
  if(from.options[x].selected == true)
  {
    /* Create the option, add to select and then add text and value.
     * We do this because IE6 and IE7 lose the text and value when
     * added to the select after the text and values have been set.
     */
    var option = from.options[x];
    var optgroup = option.parentNode;
    var optgroupMove = document.getElementById(toID + optgroup.label);
    var newOption = new Option();
    to.appendChild(newOption);
    newOption.text = option.text;
    newOption.value = option.value;

So far, we’ve just stuck the option in the select. It’s not associated with an optgroup. But, the optgroup we want may not exist. So, we check to see if it does and if not, we create one.

    // Check to see if the optgroup exists. If it doesn't create it
    if (!optgroupMove)
    {
      var optgroupNew = document.createElement('OPTGROUP');
      optgroupNew.id = toID + optgroup.label;
      optgroupNew.label = optgroup.label;
      to.appendChild(optgroupNew);
      optgroupMove = document.getElementById(toID+optgroup.label);
    }

Now that we’re sure that we have an optgroup we can append our option to it. We also then add the old option to the deletedOptions array, as well as the provided hidden variable, if it exists.

    // Add our wayward option to the optgroup
    optgroupMove.appendChild(newOption);
    deletedOptions[index++] = option;
    if(hidden)
    {
      hidden.value = hidden.value + option.value + ',';
    }
  }
}

The final piece of the puzzle is to remove the option from the originating select. To do this we simply loop over the deleted options array, removing each option from its corresponding optgroup. This block also has the code to remove the optgroup if it’s empty. This is a bit finicky in how it works though. I’ll explain more below…

for (y = 0; y < deletedOptions.length; y++)
{
  var optgroup = deletedOptions[y].parentNode;
  optgroup.removeChild(deletedOptions[y]);
  /* The following removes the optgroup from the select if it is
   * empty. I made this optional because there is odd behavior
   * in the return from hasChildNodes if there is any whitespace at
   * all within the optgroup as it was orignally defined.  White space
   * constitutes a child node.
   */
  if(DELETE_OPTGROUPS && !optgroup.hasChildNodes())
  {
    var pNode = optgroup.parentNode;
    pNode.removeChild(optgroup);
  }
}

 

The problem with deleting the otpgroups (from what I’ve gathered so far) is that the hasChildNodes function will return true if there is any whitespace within the optgroup. In other words, when you define your optgroup, it has to be on one continuous line otherwise the whitespace is reported as a childnode and hasChildNodes returns true. This isn’t an issue if you populate your select through javascript but if you do it by hand or dynamically through php, jsp, etc. you’ll most likely put the whitespace in. If you don’t, then the delete will function appropriately.

Finally, the last piece is to deselect the selects so the have no options selected.

  // Unselect all options in both "to" and "from" select
  to.options.selectedIndex = -1;
  from.options.selectedIndex = -1;
}

And that’s it. View/test the code. Enjoy!

Injured

Like flies to death the birds clouded the lot.
As I approached I saw an injured squirrel.
The birds were eating it alive.

A banshee scream drove back the birds.
They hopped about, waiting to feed,
Only a step away.

The squirrel’s hind quarters
Were a flattened penny
Left on the rails.

My coat, a stretcher
For a careless man’s act.
I tried to be gentle.

The squirrell shredded the coat like leaves
As it struggled
To be free.

I placed him in a bush
Where the fowls could not go.
Safe, but not safe enough.

Life w/out Cable and Tivo

Very soon my television viewing life will enter a dark and terrible phase. Due to family additions, the budget must be cut and the first two things to go are cable and tivo. I’m not sure how I feel about that. On the one hand, not having 70+ channels to surf on will keep me away from the television much more but the loss of the never having to remember when the shows I like are on is going to be hard to deal with. Granted, without the cable, I’ll only have broadcast shows to watch but there are several that I do enjoy watching. Heroes, My Name is Earl, and Bones to name a few. Anyway, I couldn’t tell you off the top of my head what days those shows are on so I’m either going to have to do some creative programming magic with the shell that is my Tivo box or I’m going to have to learn what nights those shows are on. Sigh..

On the plus side, I am adding another dvd to my out-at-a-time limit with Netflix. Hopefully that will keep my TV needs sated for longer periods. I’m planning on using 2 for movies and 2 for televison series (I’m watching season 5 of 24 right now). Hopefully it won’t be too bad. Of course, with the new little one, I may not get the chance to do a whole lot of TV watching but you never know.

Anyway, I’m going to go start my search for info on how to maximize the usage of my brick that was a connected Tivo service. Technically speaking, you’d think I’d be able to overhaul it somehow. I’m just not sure my technical skills are up to the task. I’ll keep you all posted on what I find…

Christmas Shopping

So, this year so far the wife and I have only gone Christmas shopping once. It was yesterday as a matter of fact and overall it went rather well. Unfortunately, I’ve been sick for the past few days so that made the experience a bit less than to be desired but as far as shopping at this time of the year it went rather well. We only ran into two overtly rude people and we were able to laugh at them without to much issue. Both individuals were at Toys ‘R’ Us.

  1. Crazy Woman with son – this woman was pretty funny. First though, I have to set the scene. Toys ‘R’ Us was a madhouse yesterday. There were people everywhere and there was almost no room in the aisles. You had to be stoked up on patience or you’d blow a fuse. So, this woman with her son. I had noticed her on a previous aisle because she was snapping at the boy over and over. He was probably nine or ten and couldn’t stop picking up the toys. Honestly, not surprising. They were in the matchbox car aisle and he just couldn’t help but look and touch. Now, it was obvious this woman was at her limit. She was completely ready to blow. Every two feet she’d snap at the boy “Put it down!” and he’d calmly set it down only to pick up something else a foot down the row. Five minutes after seeing her, crazy lady ends up behind the wife and I. Now, we’re completely stuck. There’s no where to go and nothing to do but calmly wait for things to start moving again. Well, CL decides we’re standing there doing nothing for our own amusement and barrels out into the aisle next to us in the “oncoming traffic” lane. She soon has to stop though as there’s a cart sitting there stuck waiting to get into another aisle. It takes all my self-control not to make a snide comment to the woman but I somehow manage to avoid the temptation.
  2. The other crazy lady was not as bad but was far more rude. She came barreling around a corner into our aisle. She went to our side as the side she should have gone to had people in it and nearly ran into us. She actually glared at us and then spouted “Sorry” and zoomed on. Later, in the checkout line, she came zooming by slammed her cart into ours, didn’t say a thing and practically attacked the cashier at the station she zoomed to. Very rude, and fat I might add, woman.

Again, overall it wasn’t a bad day. I didn’t have to yell at anyone or call anyone names or crash my car into anyone. I just had to calmly hold in my snide comments. Yes, things would have been more interesting if I hadn’t but when I don’t feel well and I start talking, things go downhill very quickly.

Google Web Toolkit

The Google Web Tookit Blog announced today that GWT has been released as a 100% open source product. I know many people in the OS community are probably hopping up and down with joy at this but I found myself with mixed feelings over the thing. Yes, releasing it open source is a good thing for the community and it guarantees that Google won’t do anything evil to me down the road after I’ve become entrenched in their product but it still makes me squeamish. Why? Because I’m not a huge fan of open source products. I think in the hands of capable companies like RedHat, it can be a good thing. Not necessarily is a good thing; just can be. Perhaps I’m worrying about it for no good reason but it sounds like a good way for Google to dump a product that they don’t want to support anymore. Hopefully, this isn’t the case. I really like the GWT. It’s a very handy tool. I’ve only got to use it once in a productional area but what I came up with was quite fun and GWT made it simple. So, am I excited about the opening up of GWT? Not really. Am I hopeful that, in the long run, it will be a good thing? Absolutely…

New Layout!

“What new layout?” you’re saying right now. Well, because I’m so cool, I dumped all the html tables that my pathetic WordPress theme was using and it’s now all 100% CSS driven for layout and beautifying. There’s still a few kinks to work out like spacing between posts and such but I’ve run out of steam for the evening. Plus, I’ve only tested it in Firefox and IE so there’s no guarantees that it looks good on any other browser.

Pet Peeves

Someone asked me the other day what one of my pet peeves was. Everyone in the group laughed uproariously. Evidently I have a lot of pet peeves. Oddly enough, the only one I could come up with was stupidity. Now, there’s lots of things that fall into that category so I suppose I do have lots of pet peeves. Just for fun, I thought I’d outline a few of my all time stupidity pet peeves.

Parents who always give in

Okay, this one may seem a bit vague. Let me put it in detail for you. You’re standing in line at the grocery store, Wal-Mart, Super Target, etc. and the parent (father or mother) behind you has a screaming child. The child is saying something along the lines of “I WANT IT!” over and over. The parent keeps saying “No [insert child’s name here]. You can’t have [insert product here]!” This goes on until you’re about to pay. Then you suddenly hear from the parent “Fine! You can have the damn [insert product here]! I swear! I don’t know why you can’t just accept no for an answer!” This is the point where I always want to say “He won’t accept no because he knows you ALWAYS give in.” Of course, that’s how fist fights start at the local Wal-Mart so I never do.

Customers who treat cashiers poorly

Most of the time, there’s no call to be mean/rude to the cashier at whatever establishment you’re at. Yelling at the cashier for price issues, the length of the line, the fact that your favorite brand of hair remover is out for the third time in a row, etc. All those things have nothing to do with your friendly cashier so quit treating them like trash. I used to do that job, damn it! There’s no call for abuse for those people. That said, if they’re being rude, nasty, etc. Have at it.

People who ask and then refuse what they asked for

One of my most irritating issues at my job is a group of individuals who constantly ask for more power/rights within our environment. The kicker for this is that the moment you give in and try to grant them those rights, they start screaming “That’s not our job! We can’t handle that extra responsibility! We’re not staffed for that!” I think those are the times when my foul mouth filter is pushed to the limit.

The self appointed group conscience

There’s one in every group. It’s the person who decides they need to be the moral compass for everyone within whatever group you’re in. The one who participates in the conversations until they remember they’re supposed to be a good Christian and then condescendingly tells everyone to stop being bad. Generally speaking, those people get a whole mouthful from me…

People who won’t tell me when I offend

I tend to be a foul mouthed ass who pushes the limit of what’s acceptable to say in any situation. That said, I always let everyone know if anything I say or do offends to tell me. I am very conscious of the people around me and will always be respectful (to a point) of their issues. However, if they don’t speak up, I can’t modify my behavior. One of my biggest peeves is hearing from Person A that Person B is upset because of the things I say.

Okay, this list could go on and on. I think I’ll stop for now. By the way, if anything I’ve written here offends you, too fucking bad. This is my own goddamn blog and you don’t have to read the thing so you can just fuck off.

This post was edited on 2022-11-02 to remove a questionable pet peeve that I no longer agree with and really didn’t age well. -mdc

Evil Dead

After many years of being told that I HAD to see Evil Dead, I finally rented it from Netflix. It’s been in my queue for quite a while but only got to the top because there’s nothing new I want to see right now. I have to say I was quite impressed. For an early 80’s horror flick it was very well done. I was actually cringing in horrific anticipation. It was a total blast. Wether or not I’m going to see any of the others remains to be seen. Either way, it was fun and definitely worth the time!

EDIT:? So I had to add in this extra piece. I’ve looked at the sequels (not seen yet) and I have to say I’m not sure I want to see them. From what I’ve read and the discussions I’ve had with people who’ve seen all the movies, the 2nd and 3rd movies take what was a great horror film and they turn it into more of a comedy/horror film. I really liked the first movie and am hesitant to see the others for fear it will ruin the first. So, for now, I’m not seeing the others…

Again I find myself speechless

Over and over I start blogging and over and over I stop.  It seems silly that I continue to try but I have this idea that someone out there wants to hear what I have to say. The only problem with this is that I never seem to have anything to say. Isn’t that a contradiction? So, what I’m really saying is that I honestly believe there a millions of people dying to hear me say nothing at all! Wait…so that means everyone wants me to shut up? That doesn’t make sense! For people to want me to shut up they have to know what I’ve been saying, which is nothing so why would they want me to shut up? Maybe they’re waiting with bated breath for me to say something, anything. Something profound that will rock their world! Well, guess what? Today’s post ain’t it. Night!

Napoleon Dynomite

So, Napoleon Dynamite. I’ve been hearing for awhile that this is “the greatest movie ever”, a complete waste of time, and “a turd of a movie”. In other words, depending on who I spoke with I got a different answer. The weird thing was that the people who’s opinions I trust were conflicting with each other. After getting a different response out of at least four people, I realized I had to see this movie for myself. And I did. My response? I’m just not sure.

Over all the movie was amusing though I have to admit I forced myself to watch it all. I’m the type of person that really hates movies that embarrass the main characters. I don’t find it funny and a good part of the movie focused on the embarrassing life of Napoleon. Either way, I watched the whole thing. The question remains: did I like it?

I’m going to tentatively say no. Overall, it was a waste of two hours as there wasn’t really a point to the movie. Sure, we wanted them to win the Presidency and we wanted Napoleon and Debra to hook up but there really wasn’t this driving idea behind the film. I kept wanting Napoleon to stand up and kick some ass or at least get his ass kicked but earn some respect for himself in the process. It was just a sad, amusing movie about a fucked up kid. I didn’t hate it but I really didn’t like it. 2 out of 5 stars.

« Older posts Newer posts »

© 2025 friedcherries.org

Theme by Anders NorenUp ↑