• autox420

    (@autox420)


    I have a javscript the function of it is to search for text.
    But instead of text i would like to it to search for logged in users username.

    Can someone help me out?

    <script>
    var match = '<GET_LOGGED_USERNAME>',
        firstSibling = null,
        secondSibling = null;
     
    window.onload = function(){
      var selector = document.querySelectorAll('td');
      for (i = 0; i < selector.length; ++i){
        if(selector[i].innerHTML == match){
          firstSibling = selector[i+1];
          break;
        }
      }
      
    document.write(firstSibling.innerHTML);
    }
    </script>
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m not sure where you’re going with this. Do you want to know how to tell JS what the username is so it can try to find it? Or is the idea to find a username on the page in order to know what the username is? In other words, is the username a given or the result?

    If result, the script needs to know where on the page it can expect to find the name.

    If given, pass it to JS from PHP with wp_localize_script().

Viewing 1 replies (of 1 total)
  • The topic ‘Get username in javascript’ is closed to new replies.