Search code examples
jquerycakephptwitter-bootstrapbootstrap-typeahead

how can i get attributes of textbox with typeahead of bootstrap in typeahead function call


I have used typeahead script of bootstrap with ajax on one text box , Now I have put a call as below

$('#EquipmentLogEquipmentType').typeahead({
    source: function(query, process) {
        var $url =SITE_URL+ 'api/equiplg_typefields_typeahead/' + query + '.json';
        var $items = new Array;
        var id=$(this).attr('id');      
        id=id.split('eq_');
        id=id[1];           
        $items = [""];
        $.ajax({
            url: $url,
            dataType: "json",
            type: "POST",
            data: { field_name: id},
            success: function(data) {
                console.log(data);
                $.map(data, function(data){
                    var group;
                    group = {
                        id: data.id,
                        name: data.name,                            
                        toString: function () {
                            return JSON.stringify(this);
                            //return this.app;
                        },
                        toLowerCase: function () {
                            return this.name.toLowerCase();
                        },
                        indexOf: function (string) {
                            return String.prototype.indexOf.apply(this.name, arguments);
                        },
                        replace: function (string) {
                            var value = '';
                            value +=  this.name;
                            if(typeof(this.level) != 'undefined') {
                                value += ' <span class="pull-right muted">';
                                value += this.level;
                                value += '</span>';
                            }
                            return String.prototype.replace.apply('<div style="padding: 10px; font-size: 1.5em;">' + value + '</div>', arguments);
                        }
                    };
                    $items.push(group);
                });

                process($items);
            }
        });
    },
    property: 'name',
    items: 10,
    minLength: 2,
    updater: function (item) {
        var item = JSON.parse(item);
        console.log(item.name);         
        return item.name;
    }
});

Now in above code i have mentioned $(this).attr('id'); which shows undefined ...so can anyone tell me how do i get attribute of field on which typeahead is get target?

Thanks In advance :)


Solution

  • You must use

    var id=this.$element.attr('id')); 
    
    
    
    $('#eq_equipment_type,#eq_identifier,#eq_model,#eq_manufacturer').typeahead({
        source: function(query, process) {
            var $url =SITE_URL+ 'api/equiplg_typefields_typeahead/' + query + '.json';
            var $items = new Array;
            var id = this.$element.attr('id'));     
            id=id.split('eq_');
            id=id[1];           
            $items = [""];
            $.ajax({
                url: $url,
            ...
    

    That returns the correct Id for the current typeahead input control