Jump to content

Can JS set focus on a table element when a page loads?


ginerjm

Recommended Posts

Now that I've discovered that the HTML TABLE element doesn't support a name attribute or a tabindex one, and neither do the TR or TD elements I'm wondering if JS can come to my rescue.  I'm new to it and don't find anything so far on this feature  I've used simple xx.xx.focus statements in onclick events or onload events, but since I can't set a name attribute on a table or td element, I can't do that here.

 

Any possibilities here?

tables, tr, td can't have focus since the user doens't interact with them directly. Depending on what are you trying to do with the focus event, there is another approach.

 

You can add an anchor tag inside your td and once that anchor tag receive focus, you would simulate a focus on the td visually. Here is a quick sample.

<input type="text" value="tab out of here" />
<table id="my_table" style="cursor:pointer;" onclick="do_focus(); do_click(); document.getElementById('my_anchor').focus();">
<tr>
    	<td>
        	<a href="#" id="my_anchor" onclick="do_click(); return false;" onfocus="do_focus();" onblur="do_blur();"></a>
            This is some text
        </td>
    </tr>
</table>
<input type="text" value="tab out of here" />
<script type="text/javascript">
function do_click(){
	alert('clicked');
};

function do_focus(){
	document.getElementById('my_table').style.background = "#336699";
	document.getElementById('my_table').style.color = "#ffffff";
	document.getElementById('my_table').style.border = "dotted 1px #000000";
};

function do_blur(){
	document.getElementById('my_table').style.background = "#ffffff";
	document.getElementById('my_table').style.color = "#000000";
	document.getElementById('my_table').style.border = "0px";
};

</script>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.