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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.