By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,624 Members | 2,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,624 IT Pros & Developers. It's quick & easy.

can you set focus to a custom custom element?

P: n/a
Phl
hi,

I can use the following code to set focus to standard html elements
but if I try to do the same for a custom element, it seems to complain
that it does not support a focus method. Does anyone know what is the
problem, if there is a work around?

thank you
<SCRIPT language='javascript'>
document.getElementById('" + ctrl.ID + "').focus()
</SCRIPT>
Jul 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
ki************@hotmail.com (Phl) wrote:
I can use the following code to set focus to standard html elements
but if I try to do the same for a custom element, it seems to complain
What is 'it'? A browser? If so, which one?
that it does not support a focus method. Does anyone know what is the
problem, if there is a work around?
A quick test shows that IE6 responds in exactly the same way to
document.getElementsByTagName('foo')[0].focus()
as it does to
document.getElementsByTagName('div')[0].focus()

So there seems to be no problem with applying the focus() method to
custom elements. Whether there's any effect from the user's point of
view is another matter.
<SCRIPT language='javascript'>
document.getElementById('" + ctrl.ID + "').focus()
</SCRIPT>


Can you post a URL? Without seeing the rest of your code it's hard to
tell what the problem is. The code you have there looks odd.
There's a good chance that your problem is JavaScript rather than HTML
related, in which case comp.lang.javascript would offer the best
chance for helpful advice.

Steve

--
"My theories appal you, my heresies outrage you,
I never answer letters and you don't like my tie." - The Doctor

Steve Pugh <st***@pugh.net> <http://steve.pugh.net/>
Jul 20 '05 #2

P: n/a
Phl schrieb:
I can use the following code to set focus to standard html elements
Not for all elements in all UAs as both their properties and DOMs differ.
but if I try to do the same for a custom element, it seems to complain
that it does not support a focus method. Does anyone know what is the
problem, if there is a work around?
[...]
<SCRIPT language='javascript'>
document.getElementById('" + ctrl.ID + "').focus()
</SCRIPT>


The proper syntax would be

<script type="text/javascript">
document.getElementById(ctrl.ID).focus()
</script>

This works provided that there is an element with an ID equal to the
value of ctrl.ID and document.getElementById() returns a reference to
an object that has a focus() method. So this is error-prone and one
should at least write

<script type="text/javascript">
if (typeof document != "undefined"
&& document.getElementById)
{
var o = document.getElementById(ctrl.ID);
if (o && o.focus)
{
o.focus();
}
}
</script>

(whatever ctrl.ID may be and where it may be defined).
F'up2 comp.lang.javascript (DO NOT crosspost without Followup-To!)

PointedEars
Jul 20 '05 #3

P: n/a
Phl
here is the custom element I am trying to access:

<prog:ComboBox id="insert_prjcode" name="insert_prjcode"
ParentFormID="Form1"
ResourcesDirectory="/webctrl_client/progstudios/1_2/" value="1"
size="5" onchange="__doPostBack('insert_prjcode','')">
here is the javascript which i used to access but fails , when i try
and use the onfocus method:

<SCRIPT language='javascript'>var cobj =
document.getElementById('insert_prjcode');
if(!cobj){alert('failed');}else{alert('passed');co bj.focus();}

Does this make the problem clearer?

thx
Jul 20 '05 #4

P: n/a
Phl
here is the custom element I am trying to access:

<prog:ComboBox id="insert_prjcode" name="insert_prjcode"
ParentFormID="Form1"
ResourcesDirectory="/webctrl_client/progstudios/1_2/" value="1"
size="5" onchange="__doPostBack('insert_prjcode','')">
here is the javascript which i used to access but fails , when i try
and use the onfocus method:

<SCRIPT language='javascript'>var cobj =
document.getElementById('insert_prjcode');
if(!cobj){alert('failed');}else{alert('passed');co bj.focus();}

Does this make the problem clearer?

thx
Jul 20 '05 #5

P: n/a

"Phl" <ki************@hotmail.com> wrote in message
news:55**************************@posting.google.c om...
here is the custom element I am trying to access:

<prog:ComboBox id="insert_prjcode" name="insert_prjcode"
ParentFormID="Form1"
ResourcesDirectory="/webctrl_client/progstudios/1_2/" value="1"
size="5" onchange="__doPostBack('insert_prjcode','')">
here is the javascript which i used to access but fails , when i try
and use the onfocus method:

<SCRIPT language='javascript'>var cobj =
document.getElementById('insert_prjcode');
if(!cobj){alert('failed');}else{alert('passed');co bj.focus();}

Does this make the problem clearer?


What is a custom element for, and how does a client even know how to render
it, let alone indicate that it has the focus?

Jul 20 '05 #6

P: n/a
Phl wrote:
here is the custom element I am trying to access:

<prog:ComboBox id="insert_prjcode" name="insert_prjcode"
ParentFormID="Form1"
ResourcesDirectory="/webctrl_client/progstudios/1_2/" value="1"
size="5" onchange="__doPostBack('insert_prjcode','')">
This is not HTML, so what markup language is this?
here is the javascript which i used to access but fails , when i try
and use the onfocus method:
You mean the _focus_ method.
<SCRIPT language='javascript'>var cobj =
document.getElementById('insert_prjcode');
if(!cobj){alert('failed');}else{alert('passed');co bj.focus();}
I have already posted the correct syntax and less
error-prone solution. Why don't you try that first?
Does this make the problem clearer?


Alas not. Besides, if the object has no focus() method, it cannot work.
PointedEars, F'up2 cljs

P.S.:
Please stop crossposting (without followup-To). This has nothing to do
with HTML and thus it is off-topic in ciwa.html.
Jul 20 '05 #7

P: n/a
Phl wrote:
here is the custom element I am trying to access:

<prog:ComboBox id="insert_prjcode" name="insert_prjcode"
ParentFormID="Form1"
ResourcesDirectory="/webctrl_client/progstudios/1_2/" value="1"
size="5" onchange="__doPostBack('insert_prjcode','')">

here is the javascript which i used to access but fails , when i try
and use the onfocus method:

<SCRIPT language='javascript'>var cobj =
document.getElementById('insert_prjcode');
if(!cobj){alert('failed');}else{alert('passed');co bj.focus();}

Does this make the problem clearer?

thx


You aren't using the "onfocus" method, you're using the "focus" method.
However, you aren't testing to see whether the object supports the
focus() method before you attempt to use it.

Start with this:

<script type="text/javascript">
var cobj = document.getElementById('insert_prjcode');
if (cobj) {
alert(cobj.focus);
}
</script>

If you get no alert, the object isn't being retrieved correctly.

If you get the alert(), but see anything other then:

function focus() {
[native code]
}

then the focus method isn't supported by your object. Even if you get
the text described above, the "native code" may not actually set the
focus, or it may attempt to set the focus, but other issues within the
user agent prevent that from happening. Once you have determined that
the object supports the focus() method, then the proper test for usage
would be:

if (cobj && cobj.focus) {
cobj.focus();
}

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.