?
Solved

A way to test if a form element is focusable?

Posted on 2004-09-21
5
Medium Priority
?
264 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 2000 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

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.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

585 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