简体   繁体   中英

Not able to set focus on textbox

I have a chrome extension in which I have two text boxes, one for URL and other for LDAP. Whenever user clicks on extension icon, a popup page opens and I fill the URL text box with the current page URL with the help of following code:

function setValue()
{
    var text = document.getElementById("url_textbox");
    var ldap = document.getElementById("ldap_textbox");    
    chrome.tabs.getSelected(null, function(tab) {
        text.value = tab.url;
        ldap.focus();
    });
}

The statement ldap.focus() is not working here. I am calling this function on page load.

<body onload = "setValue();">

What can I do for this?

Edit : This statement get the HTML element from the document whose Id is ldap_textbox.

var ldap = document.getElementById("ldap_textbox");

I just found a solution for this. I needed to set focus on the ldap textbox which was below the URL textbox and for some reason, in the popup page default focus is going to the first element. So, I changed the first element to the ldap textbox. :)

<script type="text/javascript">
function setValue()
{
    var text = document.getElementById("url_textbox");
    chrome.tabs.getSelected(null, function(tab) {
        text.value = tab.url;
    });
}
</script>

<html>
    <body onload="setValue();">
        <table>
        <tr>
            <td>LDAP</td><td><input type="text" id="ldap_textbox"/></td>
        </tr>
        <tr>
            <td>URL</td><td><input type="text" id="url_textbox"/></td>
        </tr>
        </table>
     </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM