[thelist] Set focus on an option (jscript)

Amazon Paulo Amazon.Paulo at Amazoniacelular.com.br
Tue Jul 16 15:03:03 CDT 2002


hello evolters,

This one is, at the same time, a tip and a question.

Firstly the question: how can I set focus on a specific option of a select?
Although there are methods to focus most elements, and also the
scrollIntoView method, which will force the page to scroll to an element,
none of them can be used by an "option" element. I have a select with
multiple options. Since many of them can be selected, and the select
contains many options, I want the page to focus on the first selected
option, so the user does not need to scroll the combo looking for it. Can I
do it?

My answer, 'till now, is "by default, no". There's no way to do it. At least
I couldn't a neat one.

But I found a dirty(?) way to do it, and it became my first tip. It comes
with an example. ;) And I hope it helps someone.

So, the tip (and of course it is worth only while the answer to my question
is no...):

<tip author="Paulo Guedes" type="setting focus on an element">

The script below will set the "selected" attribute of an option to its
value. In other words, it won't change the option's value. But, as it is
going to set this attribute, it will be forced to focus on the option.

<body onload="comboSetFocus('combo');">
	<script language=javascript>
	<!--
	function comboSetFocus(idCombo){
	var cbo = document.all(idCombo);
	cbo[cbo.selectedIndex].selected = cbo[cbo.selectedIndex].selected;
	}
	//-->
	</script>
	<select multiple size=3 id=combo>
		<option value=1>one</option>
		<option value=2>two</option>
		<option value=3>three</option>
		<option value=4>four</option>
		<option value=5>five</option>
		<option value=6>six</option>
		<option value=7>seven</option>
		<option selected value=8>eigth</option>
	</select>
</body>

</tip>




More information about the thelist mailing list