Captain Obvious is Obvious

Perhaps those more seasoned than I in the ways of javascript already know this – but I just spent 2 hours trying to fix a javascript problem that ended up having a very easy solution. My google searches came up fruitless – partially because I was using search terms to describe symptoms that I thought were related to a completely different problem – but fruitless nonetheless.

Here’s the issue. I have a simple javascript form on a hotel website – you fill in your check-in date, check-out date, number of adults and number of children, and rather than process the data and actually search, the form just builds a simple URL string and passes it off to their main corporate site (in this case, Doubletree) to use their massive internal booking engine. Easy right? All I have to do is come up with a URL string, put together with variables from the form, in a function that runs with the onClick event on the submit button – the form never actually submits. Take that URL string, throw it into a window.location, and off we go. Elegant? Notsomuch. Effective? Youbetcha.

Here’s the javascript:

function res_form() {
       
    	var rdir = "http://secure.hilton.com/en/dt/res/choose_dates.jhtml?ctyhocn=DENCHDT&arrivalDay="+document.resform.arriveDate.value+"&arrivalMonth="
+document.resform.arriveMonth.value+"&arrivalYear="+document.resform.arriveYear.value+
"&departureDay="+document.resform.departDate.value+"&departureMonth="+
document.resform.departMonth.value+"&departureYear="+document.resform.departYear.value
+"&numAdults="+document.resform.HowManyAdults.value+"&numChildren="+
document.resform.HowManyChildren.value;

window.location = rdir;
};

Simple, right? Problem is, the adults field and the children field were not getting their values passed through. Well, that’s not accurate – Firefox/Safari/Chrome were putting the values for number of adults and children in the correct places – in other words, document.form.adults.value was coming out correctly. However, in IE – (oh, boy, here we go) – this was not happening. These values were empty, which pretty much killed the URL string and the Doubletree site didn’t know what to do with it and just threw a blank screen back at me.

After much consternation and googling and bad words rattling about in my head, I decided to pick apart the form itself, piece by piece, since most of this code was something I inherited from another firm. What I found was that while non-IE browsers will take the actual value in a field and store it as document.form.field.value, IE needs a more explicit kick in the teeth, and requires that the ‘value’ attribute in the HTML<option> element be set.

So what I had was:

<select name="HowManyAdults" class="month" id="HowManyAdults">
        <option selected="selected">1</option>
        <option>2</option>
         <option>3</option>
         <option>4</option>
         <option>5</option>
         <option>6</option>
         <option>7</option>
         <option>8</option>
        <option>9</option>
         <option>10</option>
</select>

Firefox and Chrome likey, IE no likey. What I needed was:

<select name="HowManyAdults" class="month" id="HowManyAdults">
        <option value="1" selected="selected">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
      </select>

Everyone’s happy.

A very simple HTML solution to what appeared to be a javascript problem. I was convinced that those form elements were getting pushed through as null or something – but they weren’t, they just didn’t have a valid explicit value set to them, so IE had nothing to assign to them, whereas Firefox took the intuitive step of actually taking the value within the <option> tag, and making that the value.

So there you go. Probably a “duh” moment… I’m ticked that it took me two hours to figure it out, but hopefully my little odyssey helps someone else.

Mobs and Mobility

Anyone who knows me knows that I kinda make a big deal about a open source stuff… I run linux on my laptop, android on my hone, and generally prefer to stick to the free, open source model as much as possible.

Well, between having a few clients starting to ask about iPad and iphone compatibility, and a really sad state of the market for alternative pad devices (chrome os or android?Who knows…), it seemed like the best move for a foray into larger touch devices was to go ahead and get an iPad.

The ipad is my first apple device. I am still not an apple fan, and there are certainly things that irritate me, but the reality is that the apple devices dominate the market, and I have a hunch that the pad device market will more accurately resemble the mp3 player market, as opposed to the smartphone market. That is to say, i don’t think android tablets will follow the lead of the android phones… The ipad, like the ipod, is going to stay dominant in the market.

So here i am, blogging from an ipad and enjoying the functional mobility of the masses.

I still prefer android and linux though, because I am stubborn like that.

State Select List for WordPress Custom Post Type

I don’t usually blog about this stuff, but during the day I spend my time picking around WordPress trying to come up with new and cool ways to use it, both for myself and for my clients. Today, I was working on a custom plugin that required me to build a select list for all 50 of the United States. This was a bit tedious, so I’m offering up the code block as it is for you to use if you’d like. The only purpose here is to give you a block of code and save you some typing. Perhaps there’s a better way to build this list, but probably not. If you ever need a drop-down list of states to choose from in your WP custom post type or plugin, here’s a block you can start with. Now you’ll see that I have it checking for “my_fields” … I’ve dumped all of the fields for that custom post type into an array, then I’m checking to see if one of them has been saved so it keeps the correct one selected. There’s plenty more code to accomplish those functions, but this little doozy just has the

tags filled out for all 50 states. It should at least give you a good starting point. And since it’s long, you’ll have to click on the “Continue Reading” link to get to the code snippet. I’ll field any questions in the comments section if I can, but you’re better off just waiting until the plugin is complete and released into the wild, then you can have the whole thing!

Continue reading