Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

A way to test if a form element is focusable?

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

926 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