Solved

Unable to set focus on a span

Posted on 2002-07-09
11
6,864 Views
Last Modified: 2012-06-21
I'm just trying to set focus on a span, like:

<HTML><HEAD>
<SCRIPT LANGUAGE="JavaScript">
     <!--

function getFocus(which)
{
// get "null or not an object" with
// document.myForm.wantFocus.focus();

// get nothing with
 document.all.wantFocus.focus();
     
     return false;
}

//-->
</script>
</HEAD><BODY>

<form name="myForm" action="somefile.htm" method="post" onSubmit="return getFocus(this)">

<span id="wantFocus" style="text-decoration : underline; cursor : hand;">I Look like a Link and I want Focus!</span>

<table border="1"><!--- just some empty space to the Submit button --->
<tr height="700"><td>&nbsp;</td></tr></table>

<input type="submit" value="Submit">
</form>

</BODY></HTML>
0
Comment
Question by:iamari
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 22

Expert Comment

by:CJ_S
ID: 7141760
You may look like a link but you cannot have focus. Become a link, frame, input field first.

CJ

(ps. span's cannot have focus)
0
 
LVL 2

Author Comment

by:iamari
ID: 7141791
hmmm.... funny thing is, if i change it like this, it throws the "null..." error, but it works anyway
(remove the action="somefile.htm" and use  document.myForm.wantFocus.focus();
any ideas?

<HTML><HEAD>
<SCRIPT LANGUAGE="JavaScript">
     <!--

function getFocus(which)
{
// get "null or not an object" with
 document.myForm.wantFocus.focus();

// get nothing with
// document.all.wantFocus.focus();
     
     return false;
}

//-->
</script>
</HEAD><BODY>

<form name="myForm"  method="post" onSubmit="return getFocus(this)">

<span id="wantFocus" style="text-decoration : underline; cursor : hand;">I Look like a Link and I want Focus!</span>

<table border="1"><!--- just some empty space to the Submit button --->
<tr height="700"><td>&nbsp;</td></tr></table>

<input type="submit" value="Submit">
</form>

</BODY></HTML>


0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7141827
It's not a form element, therefore.

It's just not possible.

CJ
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 12

Expert Comment

by:lexxwern
ID: 7142414
yupp. i dont know what more to add.

but does your span have any for elements in them? you should give them focus directly. not through a span.
0
 
LVL 2

Author Comment

by:iamari
ID: 7143249
i thought once you id any tag element (id="wantFocus") you can address that tag in script
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7143311
Yes you can, but that does not mean that you can do everything with it. Every object has several properties and methods that can be accessed, they do not have to be the same for each and every object.

CJ
0
 
LVL 2

Author Comment

by:iamari
ID: 7143452
perhaps you're right, what throws me off is that it actually works (see the second version)
it does set focus on the span, but only if i remove the "action" property on the form tag
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7143464
Did not for me... tried it :-/

CJ
0
 
LVL 2

Author Comment

by:iamari
ID: 7143843
reason i tried <span> instead of <a href="##">:
the link just pops a new window. user selects something in new window, value is passed back to caller window.

using href will also reload the caller page. i want to avoid that.

tried <a id="wantFocus"> this doesn't load the caller, but it doesn't get focus either.

any other way to fix this?
0
 
LVL 22

Accepted Solution

by:
CJ_S earned 50 total points
ID: 7144055
<a href="#" onCLick="return false">click here, won't do anything</a>
0
 
LVL 2

Author Comment

by:iamari
ID: 7145935
thanks, CJ, it works
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Link Anchor Not working 9 67
Read text on Table 7 49
JSON help 8 49
close window in firefox 3 39
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question