View Full Version : Just an onload quickie!

01-24-2009, 05:44 PM

What I need to do is when the page loads, focus on a select option asif the user clicked the option..

I have limited ability to edit the script i'm trying to fix but essentially i have the code

<select name="country"><option value="uk">United Kingdom</option></select>

The old select menu used to have every country as a shipping option but i have managed to strip it down to 1 country. In order to add the shipping to the order you need to physically select the country (because the price which shows up on the cart page is driven by an "onchange" event i think).

So the best way i can think around this would be to select the option onload maybe?

Im not entirely sure but i bet its probably just a 1 line fix.

Like i said, i have limited ability to edit code. I can edit the raw html (parts of) of that page but i cant change any of the javascript, ajax, php etc...


http://www.sailorjerrystore.co.uk (try adding the first product in the catalogue to the cart and you will understand the shipping problem.)

Philip M
01-24-2009, 06:12 PM
<select name="country">
<option value="uk" selected>United Kingdom</option>
But clearly this will not work if the price etc. is driven by the onchange event as there has been no change.

The simplest solution (a kludge really) is probably:-

<select name="country">
<option value = "" selected>Choose a country</option>
<option value = "uk">United Kingdom</option>

Ninety-eight percent of the adults in this country are decent, hard-working, honest Englishmen. It's the other lousy two percent that get all the publicity. But then - we elected them.

01-24-2009, 07:54 PM
That is basically what i already have. I was looking for a line of javascript which would dynamically select the option "united kingdom" for me. This way, the price and the option would be selected every time the page loads.

Currently, the selected value "United Kingdom" will always return to "Please Select" whenever you refresh the page.

Any ideas?

Philip M
01-24-2009, 09:55 PM
You seem to have in mind:-

<body onload = "select()">

<select name="country" id ="country">
<option value="uk">United Kingdom</option>

<script type = "text/javascript">
function select() {
var ref = document.getElementById("country");
ref.options[0].selected = true;

But as before clearly this will not work if the price etc. is driven by the onchange event as there has been no change. You really need to alter the PHP code.