...

View Full Version : onload option(x)selected



angiras
03-03-2003, 11:21 AM
how can I select an option onload
without the onload into body tag ?

<head>
<script type="text/javascript">
function select()
{
document.onload = document.forms[0].myList.options[2].selected
}
</script>
</head>
<body>
<select name="myList">
<option value="0">zero</option>
<option value="1">one</option>
<option value="2">two</option>
</select>
</body>

does'nt work

thank you

Roelf
03-03-2003, 11:23 AM
document.onload = document.forms[0].myList.options[2].selected = true;

angiras
03-03-2003, 11:38 AM
<script type='text/javascript'>
function selection()
{
document.onload = document.forms[0].myList.options[40].selected =true;
}
</script>

I don't know why it doesn't work !

nothing is selected

angiras
03-03-2003, 11:44 AM
the complete code

-------------------------------------

<html >
<head>
<script type='text/javascript'>
function selection()
{
document.onload = document.forms[0].myList.options[2].selected =true;
}
</script>
</head>
<body>
<select name='myList' id='myList'>
<option value='0'>zero</option>
<option value='1'>one</option>
<option value='2'>two</option>
</select>
</body>
</html>

angiras
03-03-2003, 11:49 AM
yes of course FORM tags are missing . but nothing change it doesn't work

Roelf
03-03-2003, 11:56 AM
<html >
<head>
<script type='text/javascript'>
document.onload = document.forms[0].myList.options[2].selected =true;
</script>
</head>
<body>
<select name='myList' id='myList'>
<option value='0'>zero</option>
<option value='1'>one</option>
<option value='2'>two</option>
</select>
</body>
</html>

angiras
03-03-2003, 12:19 PM
document.forms[0].myList has null value or is not an object


it doesn'work (for IE 6)

Roelf
03-03-2003, 12:28 PM
then use proper form tags.



<html >
<head>
<script type='text/javascript'>
document.onload = document.forms[0].elements["myList"].options[2].selected =true;
</script>
</head>
<body>
<form>
<select name='myList' id='myList'>
<option value='0'>zero</option>
<option value='1'>one</option>
<option value='2'>two</option>
</select>
</form>
</body>
</html>

angiras
03-03-2003, 12:32 PM
no !
even getElementById ("myList") doesn't work

Roelf
03-03-2003, 12:47 PM
<html>
<head>
<script type='text/javascript'>
function selectoption() {
document.forms["myForm"].elements["myListName"].options[2].selected =true;
}
</script>
</head>

<body onload="selectoption()">
<form name="myForm">
<select name="myListName" id="myListId" size="3">
<option value='0'>zero</option>
<option value='1'>one</option>
<option value='2'>two</option>
</select>
</form>
</body>

</html>

angiras
03-03-2003, 01:25 PM
thank you for helping
but I am looking for a solution without
<body onload=">

and if I take your exemple

<script type='text/javascript'>
document.onload = document.forms["myForm"].elements["myListName"].options[2].selected =true;

</script>

doesn't work

angiras
03-03-2003, 01:55 PM
I want to select a value (no ides of index number !)

how can I do it ?

<html>
<head>
<script type='text/javascript'>
function selectOption() {
document.getElementById("myListName").options("58").selected =true;
}
</script>
</head>

<body onload="selectOption()">
<select name="myListName" id="myListId" size="3">
<option value='0'>rttr</option>
<option value='11'>hhjhj jjj</option>
<option value='58'>qq</option>
<option value='7'>ttuk gkh</option>
</select>
</body>
</html>

angiras
03-03-2003, 03:09 PM
I want to select
the value =''58


<html>
<head>
<script type='text/javascript'>
function selectOption(num)
{
var liste;
var option;

liste = document.getElementById("myList");

for(i=liste.length-1; i>=0; i++)
{
if (liste.options[i]== num)
{
option = i;
}
}
liste.selectedIndex = option;
}
</script>
</head>

<body onload="selectOption('58')">
<select name="myList" id="myListId" size="10">
<option value='0'>zero</option>
<option value='11'>onze</option>
<option value='58'>cinquante huit</option>
<option value='7'>sept</option>
</select>
</body>
</html>


doesn't work too

mordred
03-03-2003, 04:23 PM
What about this?

window.onload = function() {selectOption(59);};

angiras
03-03-2003, 04:36 PM
I have it in this way

<html>
<head>
<script type='text/javascript'>
function selectOption(liste,num) {
document.getElementById(liste).value =num;
}
</script>
</head>
<body onload="selectOption('myListName','11')">
<select name="myListName" id="myListId" size="10">
<option value='0'>tuzuk</option>
<option value='11'>onze</option>
<option value='58'>qq</option>
<option value='7'>q hhh</option>
</select>
</body>
</html>


I don't know how to avoid the body onload

thank you

mordred
03-03-2003, 04:39 PM
That works? I'd assume that you need to pass "myListId" as the first parameter, because getElementById expects a valid ID, as the name says. Anyway, try this to get rid of the onload eventhandler within the body tag:

window.onload = function() {selectOption('myListName', '11'); };

beetle
03-03-2003, 04:40 PM
Morded has it. You need to use an anonymous function when directly assigning scripting to an event property.

Not this:
document.onload = document.forms[0].myList.options[2].selected = true;

But this:
document.onload = function() { document.forms[0].myList.options[2].selected = true; }

angiras
03-03-2003, 05:06 PM
I am totally lost now , sorry but !

<html>
<head>
<script type='text/javascript'>
document.onload = function(){document.getElementById('myList').value ='11';}
</script>
</head>
<body>
<select name="myList" id="myList" size="10">
<option value='0'>tuzuk</option>
<option value='11'>here it is</option>
<option value='58'>qq</option>
<option value='7'>q hhh</option>
</select>
</body>
</html>

I don't get it

not by index , but by value

thank you for helping

timur
03-03-2003, 05:51 PM
maybe too late but

document.forms.fileUpl.nSel.selectedIndex = 0;

also works fine under MSIE 6.0

beetle
03-03-2003, 05:53 PM
Why are you trying to avoid the BODY onload?

anyhoo
window.onload = function() { setSel( document.forms[0], 'myList', '11' ); }

function setSel( f, selName, val )
{
var i, opt, selObj = f.elements[selName];

for ( i = 0; ( opt = selObj.options[&#105;] ); i++ )
if ( opt.value == val )
{
selObj.selectedIndex = i;
break;
}
}Please notice that it is window.onload, and not document.onload.

This could be done as a while loop, too.
function setSel( f, selName, val )
{
var i=0, opt, selObj = f.elements[selName];
var opt = selObj.options[i];

while( opt.value != val )
{
if ( ! ( opt = selObj.options[++i] ) );
return;
}

opt.selected = true;
}

angiras
03-03-2003, 06:11 PM
really I don't get it

I try to avoid body onload because I write the script in the head server side only IF certain conditions

abd with body onload I must write in two places

really sorry but
it doesn't work
<html>
<head>
<script type='text/javascript'>
window.onload = function() { setSel( document.forms[0], 'myList', '11' ); }

function setSel( f, selName, val )
{
var i, opt, selObj = f.elements[selName];

for ( i = 0; ( opt = selObj.options[i] ); i++ )
if ( opt.value == selVal)
{
selObj.selectedIndex = i;
break;
}
}
</script>
</head>
<body>
<select name="myList" id="myList" size="10">
<option value='0'>tuzuk</option>
<option value='11'>onze</option>
<option value='58'>qq</option>
<option value='7'>q hhh</option>
</select>
</body>
</html>

beetle
03-03-2003, 06:17 PM
An earlier quote of yours

Originally posted by angiras
yes of course FORM tags are missing . but nothing change it doesn't work How do you expect "document.forms[0]" to work without a <form> ?!?!?!?
<body>
<form>
<select name="myList" id="myList" size="10">
<option value='0'>tuzuk</option>
<option value='11'>onze</option>
<option value='58'>qq</option>
<option value='7'>q hhh</option>
</select>
</form>
</body> If you don't want a form, then use it's ID
window.onload = function() { setSel( 'myList', '11' ); }

function setSel( selId, val )
{
var i, opt, selObj = document.getElementById( selId );

for ( i = 0; ( opt = selObj.options[i] ); i++ )
if ( opt.value == val )
{
selObj.selectedIndex = i;
break;
}
}

angiras
03-03-2003, 06:26 PM
arggggggg !!!
a long fight !
it works !

thank you !

glenngv
03-04-2003, 02:46 AM
code that doesn't use getElementById, thus making it cross-browser compatible.


<html>
<head>
<script type='text/javascript'>
function selectOption(item) {
var objSelect = document.myForm.myListName;
for (var i=0;i<objSelect.options.length;i++){
if (objSelect.options[i].value==item){
objSelect.options[i].selected=true;
break;
}
}
}
window.onload = function(){selectOption('11')};
</script>
</head>
<body>
<form name="myForm">
<select name="myListName" size="10">
<option value='0'>tuzuk</option>
<option value='11'>onze</option>
<option value='58'>qq</option>
<option value='7'>q hhh</option>
</select>
</form>
</body>
</html>

beetle
03-04-2003, 03:15 AM
Uh, glenn, didn't I already do an example using form references?

glenngv
03-04-2003, 03:24 AM
oh yes, maybe i didn't read the thread carefully :D

angiras
03-04-2003, 07:24 AM
thank you to all !!!

opus1
03-04-2003, 11:29 AM
[QUOTE]Originally posted by beetle
[B]Morded has it. You need to use an anonymous function when directly assigning scripting to an event property.



I am fairly new to JavaScript and this is the first time I have heard of an 'anonymous function'. Could you please explain what it is and conditions of its usage.
OR
simply give a reference for the topic
(Should I have started a new thread for this?)

Thank you.

chrismiceli
03-04-2003, 03:10 PM
what is wrong with this


<body>
<form>
<select name="myList" id="myList" size="10">
<option value="0">0</option>
<option value="1">one</option>
<option value="2" selected="true">two</option>
</select>
</form>
</body>

angiras
03-04-2003, 03:19 PM
<body>
<form>
<select name="myList" id="myList" size="10">
<option value="0">0</option>
<option value="1">one</option>
<option value="2" selected>two</option>
</select>
</form>
</body>

beetle
03-04-2003, 04:30 PM
With an XHTML doctype, selected="true" should be just fine

opus1, go ahead and start a new thread, I don't have time at the moment to explain it...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum