0

In my Asp.Net project I have use jQuery AutoComplete in one of my textbox.

It work as expected, But one problem I found in it that when I press keydown or keyup it show the Html content in textbox.

My Code:

$(".inputText").autocomplete({
                    source: function (request, response) {
                        $.ajax({
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            url: "/URL/Path",
                            data: JSON.stringify({ prefixText: inputvalue.value }),
                            dataType: "json",
                            success: function (data) {
                                var regex = new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + request.term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi");
                                response($.map(data.d, function (item) {
                                    return {

                                        label: item.split('$')[0].replace(regex, "<b style='background-color: #efbc04;font-color:black;'>$1</b>"),
                                        val: item.split('$')[1],
                                        desc: item.split('$')[2]
                                    }
                                }))
                            },

                            error: function (result) {
                                alert("Error");
                            }

                        });
                    },
                    select: function (event, ui) {
                        $(".inputText").val(ui.item.desc);
                        __doPostBack("txtFirstName", "OnTextChanged");
                        return false;
                    }
                })
                .data("autocomplete")._renderItem = function (ul, item) {
                    return $("<li></li>")
                 .data("item.autocomplete", item)
                  .append('<a>' + item.label + '</a>')
                  .appendTo(ul);
                  };

enter image description here

2
  • I see you passing inputvalue.value in your AJAX yet I do not see where that is defined. I would suggest using request.term. Could you setup a fiddle with some example data to share? Commented Apr 21, 2017 at 17:45
  • Was able to create this following fiddle: jsfiddle.net/Twisty/awo07frf - I think the issue is that HTML elements cannot be stored in a textbox. The element expects a String for Value. That string is not rendered. I think what you want to do is use Styling to format the text in the field and not try to use Markup. Commented Apr 21, 2017 at 18:25

1 Answer 1

1

You will want to use the focus event.

Working Example: https://jsfiddle.net/Twisty/awo07frf/4/

JavaScript

$(function() {
  $(".inputText").autocomplete({
      source: function(request, response) {
        $.ajax({
          type: "POST",
          contentType: "application/json; charset=utf-8",
          url: "/echo/json/",
          data: JSON.stringify({
            prefixText: request.term
          }),
          dataType: "json",
          success: function(data) {
            var regex = new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + request.term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi");
            response($.map(data.d, function(item) {
              return {
                label: item.split('$')[0].replace(regex, "<b style='background-color: #efbc04;font-color:black;'>$1</b>"),
                val: item.split('$')[1],
                desc: item.split('$')[2]
              }
            }))
          },
          error: function(result) {
            alert("Error");
          }
        });
      },
      focus: function(event, ui) {
        $(".inputText").val(ui.item.desc);
        return false;
      },
      select: function(event, ui) {
        $(".inputText").val(ui.item.desc);
        __doPostBack("txtFirstName", "OnTextChanged");
        return false;
      }
    })
    .autocomplete("instance")._renderItem = function(ul, item) {
      return $("<li>")
        .data("item.autocomplete", item)
        .append('<div>' + item.label + '</div>')
        .appendTo(ul);
    };
});

You will see a few other changes that you may want to implement.

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

Comments

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.