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.