Solved

A way to test if a form element is focusable?

Posted on 2004-09-21
5
253 Views
Last Modified: 2008-02-01
Hi Experts

Is there a way to test if a button element can get focus()? E.g., if it is in a layer that is hidden, or an HTML block that is display:none, this usually produces a JS error. Is there a way to trap this error or test for the unfocusability of a button?

Thanks.
0
Comment
Question by:metalaureate
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:viola123
ID: 12119536
hi,

i regret to say there is no way to perform such pre-check on the so-called unfocusability.

the only walk-around is to use document.getElementById("YourButtonID"). if it returns null(in case of a hidden layer), your button is certainly unfocusable.

cheers
viola
0
 

Author Comment

by:metalaureate
ID: 12119612
Hi Viola

Alas, document.getElementById("YourButtonID") would return the  button obj irrespective of the style properties of parent elements such as layers.
0
 
LVL 6

Expert Comment

by:viola123
ID: 12119852
sorry mate, i misunderstood you, check the code below:

            <script language="javascript">
            function testfocus(){
                  if (document.getElementById("divtest").style.display = 'none')
                        alert("unfocusable");
                  else
                        alert("focusable");
            }

            </script>
      </head>
      <body onload="testfocus()">
            <div id="divtest" style="display:none"><INPUT id="button1" type="button" name="button1" value="test button"></div>      
      </body>


cheers
viola
0
 

Author Comment

by:metalaureate
ID: 12122585
Hi Viola, this requires knowing the DIV layer ID. I don't know that. I do know only this: the layer in question is the next parent layer you'll encounter if you travel up the DOM from the button, and its ID name ends in the letters "Wrap".

0
 
LVL 19

Accepted Solution

by:
dakyd earned 500 total points
ID: 12126421
It seems like you're trying to test whether an item is visible rather than whether it can have the focus.  And since you know for sure that the parentNode will determine whether or not the button is visible, you can do something like the following.  Note that if you're using stylesheets, then this approach won't work.  Regardless, hope that helps.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<script type="text/javascript">
function testVis()
{
  var inps = document.getElementsByTagName("input");
  var str = "These buttons are visible:\n";
  for (var i = 0; i < inps.length; i ++)
  {
    var par = inps[i].parentNode;
    if (par.style.display != "none" && par.style.visibility != "hidden")
      str += inps[i].value + "\n";
  }
  alert(str);
}
</script>
</head>

<body>
  <div style="visibility: visible;" id="blahtyWrap">
    <input type="button" value="Button1" />Visibility: "visible"
  </div>
  <div style="visibility: hidden;" id="someWrap">
   <input type="button" value="Button2" />Visibility: "invisible"
  </div>
  <div style="display: block;" id="otherWrap">
    <input type="button" value="Button3" />Display: ""
  </div>
  <div style="display: none;" id="fooWrap">
    <input type="button" value="Button4" />Display: "none"
  </div>

  <br />
  <a href="#" onclick="testVis(); return false;">Alert Which Buttons Visible</a>
</body>
</html>
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

776 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