skip to Main Content

my code is error td.textContent.focus is not a function

my function is like this

    var tds = document.querySelectorAll('td');
    [].forEach.call(tds, function(td) {
            
        if (td.textContent == ' Survey (Notes)') {
            td.textContent.focus();
        }
    });

when i use alert below condition of text.context is show a result
but when i’m using focus it’s show an error

i want to focus on that result when the page is loaded

thanks

2

Answers


  1. use td.focus(),even td can not be focused

    var tds = document.querySelectorAll('td');
        [].forEach.call(tds, function(td) {
                
            if (td.textContent == ' Survey (Notes)') {
                td.focus();
            }
        });
    Login or Signup to reply.
    1. By default, td elements are not focusable. You need to add the tabindex attribute to make it focusable.

    2. Focus on the td element, not textContent: Once the td element is made focusable, you can call the focus method on the element itself.

    window.onload = function() {
        var tds = document.querySelectorAll('td');
        [].forEach.call(tds, function(td) {
            if (td.textContent === ' Survey (Notes)') {
                td.setAttribute('tabindex', '-1');
                td.focus();
            }
        });
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search