-1

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);
};
3
  • Add your extra HTML only to the label, but not to the value. Commented Apr 8, 2024 at 9:54
  • Is this a JS problem or a PHP problem? What have you tried to resolve it? Commented Apr 8, 2024 at 10:35
  • it just simply remove html tags when its assign to value key yusing this $row_array["value"] = preg_replace('/<span[^>]*>.*?<\/span>/', '', $value); Commented Apr 13, 2024 at 10:36

2 Answers 2

0

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);
};
Sign up to request clarification or add additional context in comments.

Comments

0

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);
};

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.