tags:

views:

143

answers:

2

Does there exist some trick other than tabindex attribute so that a div can get focus ?

A: 

I guess, as long as you use runat="server", and give it an ID, you can access it from codebehind using [objectID].Focus().

Dunno if that's exactly what you're after.

EDIT: Just noticed that you asked about Javascript. So this might not be it.

If you want to do this on page load, you could use:

<body onLoad="document.form.div.focus()>

Where form and div of course being the IDs.

Marcus L
For one: What's the runat attribute for? Then, in `document.form.div` these are *not* IDs but the values of the name attribute. And third, the question was *not* about explicitly using JavaScript. After all, -1.
Boldewyn
For one: You need runat to be able to set an ID that the codebehind will pick up one. Then, how's the name attribute not a type of ID (yes, I may have been a bit unclear there, but I don't get how it deserves a downvote). And third, the question was tagged JavaScrip and DIV - so what's your problem?!
Marcus L
`runat` is an ASP.NET thing (proprietary Microsoft stuff which means nothing to a web browser, and in fact isn't even sent to the browser), whereas the question is about JavaScript and HTML, with no mention of anything to do with ASP.NET.
NickFitz
Yup. I noticed that, which is why I edited the post. I missunderstood the question, and I apologize for this atrocity. Let's just move on.
Marcus L
+3  A: 

Setting contenteditable="true" will also allow a div to receive focus in browsers that support it (IE 5.5+, Firefox 3.0+, WebKit for a few years, not sure about Opera), though obviously with the side effect of making the div editable by the user.

<div contenteditable="true">Tab to me</div>
Tim Down