I have a function that set selected = true. I have a dropdown list of countries. Each option has ids such as USA, GER…
function nation(arg)
{
if (arg == '<?php echo $this->escape($this->user->get( 'country' ));?>')
document.getElementById(arg).selected = true;
}
window.onload = nation('<?php echo $this->escape($this->user->get( 'country' ));?>');
I’am sure that php code returns me the correct country, but javascript debugger says:
SCRIPT5007: Unable to set value of the property 'selected': object is null or undefined
What is the problem here?
You are calling
nationimmediately and then assigning its return value toonload, so you are almost certainly calling it before the<select>has been added to the DOM.You need to assign a function.