473,379 Members | 1,255 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

can you set focus to a custom custom element?

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
7 9910
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
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
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
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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Paul Thompson | last post by:
How do I determine in JavaScript the name of the object that focus is on?
5
by: Stefano | last post by:
Hi, i want set focus element with element.focus() but i have always this exception: "focus() is not a function" Can you help me? Thanks I use Mozilla
7
by: Phl | last post by:
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...
9
by: s_m_b | last post by:
I'm trying to get an <a> element to gain the focus onload, but only get back 'has no properties'. Reading through this ng, its clear that unless the element is within a form, this doesn't happen,...
3
by: Praveen | last post by:
In IE a table element will receive focus when you either tab into it or when you click anywhere within the table. Mainly it fires the onfocus event. This doesn't happen in Mozilla (Firefox and...
3
by: VA | last post by:
t=document.getElementById('mytable') is a HTML table with some input fields in its cells Why doesnt t.getElementsByTagName('tr').firstChild.focus; put the focus on that text field? It...
0
by: Vinod. | last post by:
Hi all, I have added browser control to a windows form. I am loading pages having multiple frames. I have noticed that the focus in a text is not maintained when the application is deactivated....
2
by: yawnmoth | last post by:
Say I have two input elements and that I wanted to make it so that when the first ones input was what it should be, the focus would automatically be shifted to the next input element. ie....
4
by: Roger | last post by:
Hi, I am confused about the differences between this.window.focus(), window.focus(), and this.focus(). I want to use the calls in a <body onload="..."tag. What are the differences between...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.