A way to test if a form element is focusable?

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.
metalaureateAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
dakydConnect With a Mentor Commented:
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
 
viola123Commented:
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
 
metalaureateAuthor Commented:
Hi Viola

Alas, document.getElementById("YourButtonID") would return the  button obj irrespective of the style properties of parent elements such as layers.
0
 
viola123Commented:
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
 
metalaureateAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.