for(var i=0;i<tr.length;i++){
var td = tr[i].getElementsByTagName("td");
if(td[2].innerHTML==time && td[3].innerHTML==cls){
td[0].setAttribute("id","focus");
tr[i].style.backgroundColor="green";
var foc = document.getElementById("focus");
foc.focus();
cnt++;
}
else{
tr[i].style.backgroundColor="transparent";
}
}
for(var i=0;i<tr.length;i++){
var td = tr[i].getElementsByTagName("td");
if(td[2].innerHTML==time && td[3].innerHTML==cls){
td[0].setAttribute("id","focus");
tr[i].style.backgroundColor="green";
var foc = document.getElementById("focus");
foc.focus();
cnt++;
}
else{
tr[i].style.backgroundColor="transparent";
}
}
Share
Improve this question
edited Jul 22, 2011 at 8:54
Sk8erPeter
6,9979 gold badges50 silver badges67 bronze badges
asked Dec 19, 2009 at 6:17
teksteks
511 gold badge1 silver badge3 bronze badges
9
- 2 I don't believe you can set a focus to a TD or TR. I think you can only set the focus to an input box of some sort... possibly a link. – Tyler Carter Commented Dec 19, 2009 at 6:32
- 1 What exactly is your question? Are you trying to rewrite this with jQuery? – czarchaic Commented Dec 19, 2009 at 6:33
- 1 He wants to set the focus of the user to a tr or td. But I don't believe it is possible, as you can't set a focus to anything but a input or link. – Tyler Carter Commented Dec 19, 2009 at 6:35
- 2 I think he wants to add textbox to td and then focus it. – TheVillageIdiot Commented Dec 19, 2009 at 6:52
- 1 @Mark: If its not unique some browser implementation will fail to locate particular element – Xinus Commented Dec 19, 2009 at 6:54
3 Answers
Reset to default 9This is a year too late, but you CAN focus to a TD element. Just give it a tabindex property, and then do a focus().
This works for DIV elements and almost all others.
I've tried this on Firefox 3.5 and up.
As the question is about changing the focus on a TD or TR, then the answer is that you cannot focus on such HTML tags; you can change the currently focused element if that is a input element (textarea, textfield, checkbox, radios) or a link, which are highlighted in a particular way when you click the key tab.
If you are really trying to focus on an input field, as suggested by Jonathan Sampson, then the answer he gave is the correct one.
In your code you are really trying to get the focus on a tag TD, then the answer to the question is no.
It appears you have an input field with the id "focus" within a td, so I'm assuming you want to focus on that input itself. With jQuery, you would do this:
$("input#focus").focus();
This will cause the focus to be brought to that particular element once called. I see that you also tried setting the ID of a particular TD to "focus" as well; you should only use a unique ID value once per page. If you feel the need to use a single value to represent many elements, consider using classnames instead as there can exist any number of them on a page.