skip to Main Content

I am using jquery ui autocomplete to fetch cars,parts,and services from my database, i am diffrentiating both or three records type by adding span in last. But when i select any value its showing span html code in input with code how can i fix that ?

php:

public function partCodeAutoc(Request $request)
{
        $return_arr1 = [];
        $return_arr2 = [];
        $return_arr3 = [];
        $type = $request->code_type;
        $part_code = $request->term;
        $column = "code";
        $column2 = "car_code";

        $query = Cars::where($column2, "LIKE", "%" . $part_code . "%")->where('car_status',3)->get();
        if ($query->count() > 0) {
            foreach ($query as $rows) {
                $newdata = $rows->$column2.' <span class="badge badge-light-secondary" style="float:right;">Car</span>';
                array_push($return_arr1, $newdata);
            }
        $return_arr2 = array_unique($return_arr1);
        foreach ($return_arr2 as $key => $value) {
            $row_array["label"] = $value;
            $row_array["value"] = $value;
            array_push($return_arr3, $row_array);
        }
        echo json_encode($return_arr3);
}

js:

$("#part_code").autocomplete({
    source: function (request, response) {
      if ($("#service-chk").is(":checked")) code_type = 1; 
      else code_type = 0;
      $.ajax({
        url: "partCodeAutoC",
        type: "POST",
        dataType: "json", 
        data: {
          code_type:code_type,
          term: request.term,
        },
        success: function (data) {
          response(data);
          },
      });
    },
    minLength: 1,
    select: function (event, ui) {

    },
}).data("ui-autocomplete")._renderItem = function (ul, item) {
    return $("<li>")
        .append("<div>" + item.label + "</div>")
        .appendTo(ul);
};

2

Answers


  1. You are seeing the span tag because you didn’t delete them, which you have to do.

    This is how you can modify it:

    $("#part_code").autocomplete({
        source: function (request, response) {
            if ($("#service-chk").is(":checked")) code_type = 1; 
            else code_type = 0;
            $.ajax({
                url: "partCodeAutoC",
                type: "POST",
                dataType: "json", 
                data: {
                    code_type: code_type,
                    term: request.term,
                },
                success: function (data) {
                    response(data);
                },
            });
        },
        minLength: 1,
        select: function (event, ui) {
            // Extracting text without HTML tags
            var selectedValue = ui.item.value.replace(/<[^>]+>/g, ''); 
            // Set the input value to the extracted text
            $(this).val(selectedValue); 
        }
    }).data("ui-autocomplete")._renderItem = function (ul, item) {
        // Render each item without HTML tags
        return $("<li>")
            .append("<div>" + item.label.replace(/<[^>]+>/g, '') + "</div>")
            .appendTo(ul);
    };
    
    Login or Signup to reply.
  2. This way, the HTML tags will not be included when an item is selected

    $("#part_code").autocomplete({
        source: function (request, response) {
            if ($("#service-chk").is(":checked")) {
                code_type = 1; 
            } else {
                code_type = 0;
            }
            $.ajax({
                url: "partCodeAutoC",
                type: "POST",
                dataType: "json", 
                data: {
                    code_type: code_type,
                    term: request.term,
                },
                success: function (data) {
                    response(data);
                },
            });
        },
        minLength: 1,
        select: function (event, ui) {
        }
    }).data("ui-autocomplete")._renderItem = function (ul, item) {
        var $li = $("<li>");
        var labelWithoutTags = item.label.replace(/</?[^>]+(>|$)/g, "");
        $li.append("<div>" + labelWithoutTags + "</div>");
        return $li.appendTo(ul);
    };
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search