Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to focus a <tr> or <td> tag using jQuery or JavaScript?

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";
    }

 }
like image 437
teks Avatar asked Dec 19 '09 06:12

teks


2 Answers

This 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.

like image 154
Vanco Avatar answered Oct 16 '22 21:10

Vanco


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.

like image 31
apaderno Avatar answered Oct 16 '22 21:10

apaderno