简体   繁体   中英

Chrome Extension Textfield enter checking

How do you check if the user of the extension presses ENTER while in a textfield ( search ), kind of like pressing a submit button but just pressing enter. I use the following code for button presses

//action listener
document.addEventListener('DOMContentLoaded', function () {
    document.getElementById('search').addEventListener('click', searchResult);
    document.getElementById('link').addEventListener('click', searchResult);
});

would I have to use somthing like document.getElementById('search').addEventListener('enter', searchResult); ?

This one should do the trick:

document.addEventListener('DOMContentLoaded', function () {
    var searchElement = document.getElementById('search');

    searchElement.addEventListener('click', searchResult);
    searchElement.addEventListener('keydown', function(e) {
        if (e.keyCode == 13) {
            searchResult();
        }
    });
    document.getElementById('link').addEventListener('click', searchResult);
});

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