Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I set focus on an element which is not a link?

Status
Not open for further replies.

unigodx

Programmer
Jan 1, 2009
8
0
0
Normally, I can set focus on a link element ( <a heref="..." ) by just getting that element by ID and then use focus().

However, I have to set focus on an element that is not a link. It has a structure as followed..

<span class="mw-headline">History</span>

Can anybody please suggest me a solution? I can write only the script but cannot do anything directly to the body of the web page.

My intention is to set a temporary focus on that element, which is in the middle of a very very long page full of texts. So I can just press [Tab] and instantly move to a link nearest to that span element

Thank You.
 
Sorry. The missing word is press 'tab button' and instantly...
 
Hi

You can not.

But while your reason is to reach a link, why not focus that directly ? I would use a function like this :
Code:
function focusafter(what)
{
  var thespan=document.getElementById(what)
  if (!thespan) return

  var alltag=document.getElementsByTagName('*')

  var found=false
  for (var i=0,l=alltag.length;i<l;i++) {
    if (alltag[i]==thespan) found=true
    else if (found && alltag[i].tagName=='A') {
      alltag[i].focus()
      break
    }
  }
}
Tested in FireFox, SeaMonkey, Opera, Safari, Chrome, Konqueror and Explorer.

Feherke.
 
Hi

Thinking again and again to your request, I am still in dark.

Why would you like that ?
- To force scrolling to a given [tt]span[/tt] element ? Better use the [tt]scrollIntoView()[/tt] method ( see MDC or MSDN reference )
- To emphasize a given [tt]span[/tt] element ? Better apply some styling to that element using JavaScript.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top