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"; } } 
+4
source share
3 answers

It's already too late, but you can focus on the TD element. Just give it the tabindex property and then do focus ().

This works for DIV elements and almost all others.

I tried this on Firefox 3.5 and above.

+8
source

In connection with the question of changing focus to TD or TR, the answer is that you cannot focus on such HTML tags; you can change the current focused element if it is an input element (textarea, text field, check box, radio) or a link that are highlighted in a certain way when you press the key tab.
If you are really trying to focus on the input field, as suggested by Jonathan Sampson, then the answer he gave is correct.

In your code, you are really trying to focus on the TD tag, then the answer to the question is no.

+2
source

It seems you have an input field with the identifier "focus" inside td, so I assume that you want to focus on this very input. Using jQuery you will do the following:

 $("input#focus").focus(); 

This will cause the focus to be shifted to that particular element that was once called. I see that you also tried to set the identifier of a specific TD to "focus"; you should only use the unique identifier value once per page. If you think you need to use a single value to represent many elements, consider using class names instead, as there can be any number of them on a page.

+1
source

All Articles