0

I am posting html and jquery code. Using JQuery 1.9.1.

SCRIPT

$(document).ready(function () {
    $('#search').keyup(function () {

        var search = $('#search').val();

        if (search.length > 2) {
            var s = $('<select />');
            $.ajax({
                type: "POST",
                url: "searchuser",
                cache: false,
                data: 'search=' + $("#search").val(),
                success: function (response) {

                    $.each(response, function (index, value) {
                        $('<option />', {
                            value: value,
                            text: value
                        }).appendTo(s);
                    });

                },
                error: function (e) {
                    alert('Error: ' + e);
                }

            });
        }

        s.appendTo('body');
    });
});

HTML

    <form>
      <input id="search" type="text"  name="search" />
    </form>
    <div id="info"></div>

    <div id="other">
      Trigger the handler
    </div>

and the above code is creating multiple select elements I understood that it is due to ajax calls but how can i avoid creating additional select elemnts or suggest me how can i convert text box to select item

13
  • When you post code, giving a jsfiddle link is always a good way to get faster answers :) Commented May 31, 2013 at 9:05
  • i can not because i am having a request which is coming from database so it will post error, but just for clear understanding of java script and html i am posting my jsfiddel link jsfiddle.net/Pkgje Commented May 31, 2013 at 9:18
  • in jsfiddle you have some nice echo framework for ajax request, i will post you one Commented May 31, 2013 at 9:25
  • there you have it, a fiddle that will spoof the ajax request : http://jsfiddle.net/techunter/VLvDu/ Commented May 31, 2013 at 11:11
  • i kept my java script and body here jsfiddle.net/kPjTZ/1 i am not understanding why ajax is not called can you help me @TecHunter Commented May 31, 2013 at 13:06

1 Answer 1

1

You need to create the select element only once then append everything OR you can use .replaceWith.

If you don't especially need to create the select element on the fly it's even better to put in directly in the html :

<body>

    <form>
      <input id="search" type="text"  name="search" />
    </form>
    <div id="info"></div>

    <div id="other">
      Trigger the handler
    </div>
    <select id="searchSelectTarget">
    </select>

</body>

While in your JS be carefull with Ajax, it's asynchronous so most of the time the .appendTowill be executed before ajax returns :

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

    var search = $('#search').val();

    if (search.length > 2) {
        var $select = $('#searchSelectTarget').empty(); //just to reset the content
        $.ajax({
            type: "POST",
            url: "searchuser",
            cache: false,
            data: 'search=' + $("#search").val(),
            success: function (response) {

                $.each(response, function (index, value) {
                    $('<option />', {
                        value: value,
                        text: value
                    }).appendTo($select);
                });
//    s.appendTo('body'); this one would have moved here but you don't need it anymore
            },
            error: function (e) {
                alert('Error: ' + e);
            }

        });
    }


});
Sign up to request clarification or add additional context in comments.

8 Comments

thank you very much @TecHunter it is working fine but can i convert the input tag to selectbox
i want to create a selectbox with same name and id as input field can i
@user1654561 you mean when your request comes back delete the input and replace it with the select?
yeah exactly as my request has a list of elements show that i can show them as suggestion to the user to select
what i want to do exactly is i want the user to enter text as well as select from combobox @TecHunter
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.