locked
How to get the source element id on select function in autocomplete RRS feed

  • Question

  • User-712926555 posted

    Hi,

    I need to get Source Id in select event

    $(function () {
                $(".AtComplete").autocomplete({
                    source: function (request, response) {
                        var show_id = this.element.attr("id");
                        var name = $('[id$=' + show_id + ']').val();
                        $.ajax({
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            url: "Purchase_Product.aspx/GetAutoCompleteDataByName",
                            data: "{'name':'" + name + "'}",
                            dataType: "json",
                            success: function (data) {
                                response(data.d);
                            },
                            error: function (result) {
                                alert("Check Name");
                            }
                        });
                    },
                    select: function (event, ui) {
                        $.ajax({
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            url: "Purchase_Product.aspx/GetProductByCode",
                            data: '{"Code":"' + ui.item.value + '"}',
                            dataType: "json",
                            success: function (data) {
                               // Here I need to access Source Id or show_id Value
                                alert($(this).attr('name'));
                               
                            },
                            error: function (result) {
                                alert("Error");
                            }
                        });
    
                    }
                });
            });

    I have tried 

    $(this).attr('name')

    event.target.name

    $(this).prop('name')

    But everything returns empty value...

    Wednesday, June 1, 2016 12:21 PM

Answers

  • User61956409 posted

    Hi Hisanth,

    You could define a global variable “show_id”, then you could assign value to “show_id” and access the variable on select event.

    <script>
        $(function () {
            var show_id = "";
    
            $(".AtComplete").autocomplete({
                source: function (request, response) {
                    show_id = this.element.attr("id");
                    var name = $('[id$=' + show_id + ']').val();
    
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "Purchase_Product.aspx/GetAutoCompleteDataByName",
                        data: "{'name':'" + name + "'}",
                        dataType: "json",
                        success: function (data) {
                            response(data.d);
                        },
                        error: function (result) {
                            alert("Check Name");
                        }
                    });
                },
                select: function (event, ui) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "Purchase_Product.aspx/GetProductByCode",
                        data: '{"Code":"' + ui.item.value + '"}',
                        dataType: "json",
                        success: function (data) {
    
                            // Here I need to access Source Id or show_id Value
                            //alert($(this).attr('name'));
    
                            //here, you could get the variable "show_id"
    
                        },
                        error: function (result) {
                            alert("Error");
                        }
                    });
    
                }
            });
        });
    </script>
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 2, 2016 2:31 AM