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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:01:56+00:00 2026-06-18T22:01:56+00:00

I have this code, which adds dynamic div right below the textbox but I

  • 0

I have this code, which adds dynamic div right below the textbox but I want to remove it after the user deletes the characters:

The code below doesn’t work, I don’t see what I am doing wrong

Code from Fiddle

See code below:

    $(document).ready(function () {

        $("#searchcontent").keypress(function (e) {
            var dvSearch = $('<div />');
            dvSearch.attr('dvSearch', '1');

            if ($("#searchcontent").val().length >= 2) {
                var pos = $(this).position();
                dvSearch.html("<div>ffff:<div><div>eeee:<div><div>ggggg:<div>").css({
                    top: pos.top + $(this).height() + 18,
                    left: pos.left,
                    width: '300px',
                    position: 'absolute',
                    'background-color': 'Yellow'
                }).insertAfter($(this));
            }
            else {
                $("div[dvSearch='1']").remove();
            }
        });

    });
  • 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-18T22:01:57+00:00Added an answer on June 18, 2026 at 10:01 pm

    I would suggest simply having a container ready to receive the result content in your HTML. Then you can reduce the problem by either adding the results there or emptying that container. In your original code, you were continually adding div elements on every keypress event.

    Also, as others have mentioned, you will want to use .keyup() instead of .keypress() to ensure the value of the input is correct when the event triggers.

    HTML

    <input type="text" id="searchcontent" name="searchcontent" class="txtSearch" maxlength="30" />
    <div id="searchresult"></div>
    

    JS

    $(document).ready(function() {
        $("#searchcontent").keyup(function(e) {
            if ($("#searchcontent").val().length >= 2) {
                var pos = $(this).position();
                $("#searchresult").html("<div>ffff:<div><div>eeee:<div><div>ggggg:<div>");
            }
            else {
                $("#searchresult").empty();
            }
        });
    });
    

    jsfiddle

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

Sidebar

Related Questions

I have this code which adds a x number of views to a Multiview
I have this code, which creates an image, and then adds some effects to
I have this code which gets WP posts, but it gets all the posts
I have this code which compiles and works as expected: class Right {}; class
I have the following code which adds some arrays to a hashmap but then
I have this code, which adds a line into my Students.txt, however, everytime I
I have this JS code which adds some HTML inside a tbody : thestring='<tr><td><input
I have this piece of code which creates a note and adds to the
I have this code which is pretty easy, it adds an image to the
I have this code, which adds a extra table row when clicked and hides

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.