• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Question about keyboard focus and nested DIVs...

Ichinisan

Lifer
I have nested DIVs with visible borders. Generally, a DIV will contain one object (text box, check box, or yes/no radio button), so I want to improve accessibility by making it so the user only has to click within the DIV to give focus to the object. I know of two ways to give focus to an object within the DIV:
1: <label for="whatever">, wrapped around the DIV
2: <div onclick="document.getElementById('whatever').focus()">

HTML:
<html>
    <head>
        <title>Demonstrate Focus Problem with Nested DIVs</title>
        <style type="text/css">
            body{font-family:"Arial";}
            div{border:2px solid #a1a1a1;}
            div.main{
                padding:20px 20px; 
                width:550px;
                border-radius:25px;
                -moz-border-radius:25px; /* Firefox 3.6 and earlier */
            }
            div.d-outer{
                padding:15px 15px; 
                background:#dddddd;
                width:520px;
                border-radius:25px;
                -moz-border-radius:25px; /* Firefox 3.6 and earlier */
            }
            div.d-inner{
                border:1px solid #a1a1a1;
                padding:15px 15px; 
                background:#dddddd;
                width:490px;
                border-radius:25px;
                -moz-border-radius:25px; /* Firefox 3.6 and earlier */
            }
        </style>
    </head>
    <body>
        <label for="firstthing">
			<label for="firstthing">
            <div id="firstdiv" class="d-outer">
				Thing one:<br>
                <input id="firstthing" name="firstthing"> *<br>
            </div>
			</label>
			<br>
			<label for="secondthing">
            <div id="seconddiv" class="d-outer">
				Thing two:<br>
                <input id="secondthing" name="secondthing"> *<br>
            </div>
			</label>
			<br>
			<label for="thirdthing">
            <div id="thirddiv" class="d-outer">
				Thing three:<br>
                <input id="thirdthing" name="thirdthing"> *<br>
				<br>
				<label for="fourththing">
                <div id="firstinnderdiv" class="d-inner">
					Thing four:<br>
                    <input id="fourththing" name="fourththing"> *<br>
                </div>
				</label>
				<br>
				<label for="fifththing">
                <div id="secondinnerdiv" class="d-inner">
					Thing five:<br>
                    <input id="fifththing" name="fifththing"> *<br>
                </div>
				</label>
            </div>
			</label>
        </label>
    </body>
</html>

Method 1 works fine in Internet Explorer, but Google Chrome does something strange. Clicking in the DIV area gives focus to the text box, but clicking directly in the text box won't select it!

Anyway, I want the outer DIV to contain a yes/no check box that controls whether-or-not the inner DIVs appear. I can't use the <label> tag to give focus to the first check box, because that will also make a selection. I have to use onclick and .focus().

However, that method gets even more messed-up because onclick for an inner DIV still triggers the onclick for the outer DIV.

Is there a more elegant way to do this?

Thanks!
 
This is a job for jquery!

Code:
$('#firstdiv').click(function() {
  $('#firstthing').focus();
});

or even

Code:
$('div').has('input').click(function() {
  $(this).find('input').focus();
});

or even a 3rd way

Code:
$('input').parent('div').click(function() {
  $(this).find('input').focus();
});
 
Last edited:
Back
Top