Solved

A way to test if a form element is focusable?

Posted on 2004-09-21
5
251 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now