简体   繁体   中英

How to generate click event of anchor tag on enter key press in textbox

I am developing a JSP page where I have many anchor tags. So I have put a text box where I can search all the anchor tags on my page. I am using Javascript.

Now what I need is when I text an anchor tag name in the text box and press Enter Key it should work as onclick of that particular anchor tag .

I have tried the below code but it did not work for me:

<script>
function searchkeypress(e)
{
if(typeof e=='undefined'&& window.event){e=window.event;}
if(e.keycode==13)
document.getElementById("search").click();
}
</script>

<input type="text" id="text1" onkeypress="searchkeypress(event)"/>
<a id="search" href="#Ajax" onclick="index.jsp"/>
<input type="text" id="txtSearch" onkeydown="if (event.keyCode == 13) document.getElementById('index').click()"/>
<a id="index" href="index.jsp"/>

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