Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7751095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:27:36+00:00 2026-06-01T11:27:36+00:00

I have a bit of JavaScript that builds some HTML for me and inserts

  • 0

I have a bit of JavaScript that builds some HTML for me and inserts it into a div. I am using jQuery 1.7.2 for this test.

I’m interested in attaching a custom change or keyup event handler on an input text field called gene_autocomplete_field.

Here’s what I have tried so far.

The following function builds the HTML, which is inserted into a div called gene_container:

function buildGeneContainerHTML(count, arr) {
    var html = "";
    // ...
    html += "<input type='text' size='20' value='' id='gene_autocomplete_field' name='gene_autocomplete_field' placeholder='Enter gene name...' /><br/>";
    // ...
    return html;
}

// ...
$('#gene_container').html( buildGeneContainerHTML(count, geneNameArr) );

In my calling HTML, I grab the gene_autocomplete_field from the gene_container element, and then I override the keyup event handler for gene_autocomplete_field:

<script>
    $(document).ready(function() {
        $("#gene_container input:[name=gene_autocomplete_field]").live('keyup', function(event) {
            refreshGenePicker($("#gene_container input:[name=gene_autocomplete_field]").val());
        });
    });
</script>

When I change the text in gene_autocomplete_field, the refreshGenePicker() function just sends an alert:

function refreshGenePicker(val) {
    alert(val);
}

Result

If I type any letter into the gene_autocomplete_field element, the event handler seems to call alert(val) an infinite number of times. I get one alert after another and the browser gets taken over by the dialog boxes. The value returned is correct, but I worry that refreshGenePicker() gets called over and over again. This is not correct behavior, I don’t think.

Questions

  • How do I properly capture the keyup event once, so that I only handle a content change to the autocomplete field the one time?
  • Is there a different event I should use for this purpose?

UPDATE

It turns out that more than just a keyCode of 13 (Return/Enter) can be an issue — pressing Control, Alt, Esc or other special characters will trigger an event (but will be asymptomatic, as far as the infinite loop issue goes). The gene names I am filtering on do not have metacharacters in them. So I made use of an alphanumeric detection test to filter out non-alphanumeric characters from further event handling, which includes the Return key:

if (!alphaNumericCheck(event.keyCode)) return;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:27:37+00:00Added an answer on June 1, 2026 at 11:27 am

    alert is called infinite times because you use the ‘Enter’ key to confirm/dismiss the alert. Use .on('change') instead. This will prevent refreshGenePicker from being called when you use enter in an alert.

    • JSFiddle demonstration using keyup (Click on OK to prevent infinite alerts).
    • JSFiddle demonstration using change

    However, the 'change' event will only trigger if the input element looses focus. If you want to use refreshGenePicker on every key, use the following approach instead:

    $("#gene_container input:[name=gene_autocomplete_field]").live('keyup', function(event) {
        if(event.keyCode === 13) // filter ENTER
            return;
        refreshGenePicker($("#gene_container input:[name=gene_autocomplete_field]").val());
    });
    

    This will filter any incoming enter keyup events (jsFiddle demo). Also switch to .on and drop .live.

    EDIT: Note that there are more possibilities to dismiss an alert modal, such as the escape or space key. You should add a check inside your refreshGenePicker whether the value has actually changed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this bit of javascript written with jQuery 1.2.5. It's contained inside the
I have problems with the following bit of javascript/jquery code: this.droppable = function(){ $('.imageWindow
I'm pulling my hair out over this. I have this bit of javascript that
I have this bit of javascript: var jsonString = some string of json; $.post('proxy.php',
I have a bit of javascript code that is getting loading in some data
So, I have a small bit of javascript code that makes a popup div.
I have a bit of Javascript code that creates a save friendly version of
I have a little bit of Javascript that almost works correctly. Here's the code:
I'm a bit of a javascript noob, but I have a general question that
I have this bit of JavaScript... 15 $('.ajax_edit_address').each(function() { 16 $(this).ajaxForm({ 17 target: $(this).parents('table.address').find('tr.address_header').children(':first'),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.