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 8149855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:56:06+00:00 2026-06-06T14:56:06+00:00

Im trying to add the text contained inside an <li> element when the user

  • 0

Im trying to add the text contained inside an <li> element when the user clicks on it to an input field #private_users. The problem is that if the list looks like this:

<ul>
<li class="user_li_selection">Jane</li>
<li class="user_li_selection">John</li>
<ul>

And then the user clicks on ‘John’, the string that gets puts into the input box looks like John, Jane, John. Then, after this first selection, the script behaves normally attaching only the selected names. Basically, it seems like the script is attaching the text contained in all of the li elements and then attaching the text of the one selected. I cant’t figure why this is happening. It may be useful to note, that the <li> elements are being attached dynamically with a different script.

$(document).on('click', '.user_li_selection', function(e) {
             e.preventDefault; 
             var selection = $(this).html();
             var current_string = $('#private_users').val();

             arr = current_string.split(','), i;
             length =  arr.length;

             if (length != 1) {
             arr[length-1] = ' '+selection+', ';
             } else {
             arr[length-1] =  selection+', ';
             }

             $('#private_users').val(arr);

             //$('.input_auto_complete').remove();

          });



     $('#private_users').keyup(function() {

        var input = $(this).val();

        arr = input.split(', '), i; 
        length =  arr.length;
        var current_input = arr[length-1];

        var input_box = $('#private_users');
        var position = input_box.position();

        if ($('.input_auto_complete').length == 0 && input !== '') {
            $('body').append('<div class="input_auto_complete"></div>');
        }

        if (input == '') {
            $('.input_auto_complete').remove();
        }

        $('.input_auto_complete').css('top',position.top + 24);
        $('.input_auto_complete').css('left',position.left);
        $('.input_auto_complete').show();

        $.post('/username-query', {
        'user_input': current_input
        }, function(data) {
        $('.input_auto_complete').empty().append(data);
        }); 

     });

     $('#private_users').blur(function () {

        if ( !$('.user_li_selection').click() ) {
         $('.input_auto_complete').remove();
        }

     });

The input field I am targeting is just a simple text input like this:

<input type="text" id="private_users" />

JS FIDDLE EXAMPLE: (MAKE SURE to SELECT THE INPUT FIELD before selecting an item from the list or the problem will not occur.)

  • 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-06T14:56:08+00:00Added an answer on June 6, 2026 at 2:56 pm

    The culprit is this line:

    if ( !$('.user_li_selection').click() )
    

    when the text box looses its focus, you simulate a click on all list elements.

    I’m not sure what you are trying to do with this line, the if statement does not really make sense.

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

Sidebar

Related Questions

I'm trying to add an additional text field to magento's product review. It looks
I'm trying to add a class (.active) to a text field once the user
I 'm trying to add text on a specific image. Its working perfectly but
I'm trying to add text to textarea in current pointer position, here's the code
I am trying to add full text search capabilities to my RoR app, but
I'm trying to add a plain text node in a zend form - the
Hey guys, I'm trying to add a textfield.text entry to an array. I want
I'm trying to add some HTML formatted text to Word using Office Interop. My
I am trying to add a Jquery calendar picker to a text box at
I'm trying to add an animation to my ListView so text will fade into

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.