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

  • SEARCH
  • Home
  • 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 3978740
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:05:06+00:00 2026-05-20T05:05:06+00:00

I have this javascript/ jquery to list available users. HTML: <input name=username type=text size=16

  • 0

I have this javascript/ jquery to list available users.

HTML:

<input name="username" type="text" size="16" onkeyup="lookup(this.value);" onblur="fill();"/>                                   
<div class="suggestionsBox" id="suggestions" style="display: none;">
<img src="images/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
<div class="suggestionList" id="autoSuggestionsList">
&nbsp;
</div>
</div>

jquery:

<script type="text/javascript">
function lookup(username) {
    if(username.length == 0) {
    // Hide the suggestion box.
        $('#suggestions').hide();
    } else {
        $.post("php/rpc.php", {queryString: ""+username+""}, function(data){
            if(data.length >0) {
                $('#suggestions').show();
                $('#autoSuggestionsList').html(data);
            }
        });
    }
} // lookup

function fill(thisValue) {
    $('#username').val(thisValue);
    setTimeout("$('#suggestions').hide();", 200);
}
</script>

This gives a list of available usernames that you can type in.

example HTML:

<li onclick="fill('user1');">user1</li>
<li onclick="fill('user1');">user1</li>

But the fill function does not work. If I add alert('test') a popup shows up, so it works, but it does not fill out the input field username

  • 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-05-20T05:05:06+00:00Added an answer on May 20, 2026 at 5:05 am

    The main bug here is that the name attribute of an input element is not the same as an id attribute. Either

    <input name="username" type="text" size="16" onkeyup="lookup(this.value);" onblur="fill();"/>
    

    should be

    <input id="username" name="username" type="text" size="16" onkeyup="lookup(this.value);" onblur="fill();"/>
    

    or the jQuery selector referencing it should be $("input[name='username']").

    The other possible bug – and it really depends on how you’re declaring the javascript – is whether the AJAX-loaded HTML can access the fill function. One easy way to fix this, though you may lose out somewhat in performance if you’re loading a large number of list elements, is to assign the event handler in the callback function:

         $.post("php/rpc.php", {queryString: username}, function(data){
            if(data.length >0) {
                $('#suggestions').show();
                $('#autoSuggestionsList').html(data)
                    // find list elements and attach the event handler
                    .find('li').click(function() {
                        // this removes the need to pass in any arguments:
                        // just use the text of the list element
                        var val = $(this).text();
                        $('#username').val(val);
                        // as @SLaks notes, better a function than a string here
                        setTimeout(function() {
                            $('#suggestions').hide();
                        }, 200);
                    });
            }
        });
    

    This removes the need for two functions, and makes the HTML you load with AJAX simpler, since you can just load

    <li>user1</li>
    <li>user2</li>
    

    See http://jsfiddle.net/nrabinowitz/WNJnc/3/ for a full working example of this code.

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

Sidebar

Related Questions

No related questions found

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.