$('.Find').autocomplete(...">

How to get id from class selector

<input  id="1" class="Find">
<input  id="2" class="Find">
<input  id="3" class="Find">

$('.Find').autocomplete('auto/data.php?mode='+ $('.Find').attr('id')  +'', {    
                    width: 220,
                    max: 5,
                    selectFirst: false,      
                    mustMatch:true,
                    formatItem: function(data, i, n, value) {return  value.split("|")[0];}
                    });

      

In this case, the code $('.Find').attr('id')

only shows the first array. Don't show specific id where i need to.

I changed with a new variable var n

this line $('.Find').attr('id')

with a new variable, but the result undefined

.

The new var looks like this: var one = $("#GetObjc").attr("value");

Can someone help me. What should I do to get the specific id of this class. Sorry if I'm confused, but I'm a beginner.

+3


source to share


6 answers


I'm not sure what you are looking for. but you can get everything id

with .each

.

var ids ='';
$(".Find").each(function( index, value ) {
  ids += $(this).attr('id') + ',';
});

      

Then you can split the variable ids

or something like that.



Check out JSFiddle demo

In your case, I find this useful for you:

$('.Find').each(function() {
    var $this = $(this);
    $this.autocomplete('auto/data.php?mode='+ $this.attr('id')  + '', {    
        width: 220,
        mzx: 5,
        selectFirst: false,      
        mustMatch:true,
        formatItem: function(data, i, n, value) {return  value.split("|")[0];}
    });
});

      

+1


source


2% chance I understood your question correctly, but it seems that you need to iterate over the list of items:



    $('.Find').each(function() {
        $(this).autocomplete('auto/data.php?mode='+ $(this).attr('id')  +'', {
            width: 220,
            max: 5,
            selectFirst: false,      
            mustMatch:true,
            formatItem: function(data, i, n, value) {return  value.split("|")[0];}
        });
    });

      

+1


source


I think you want to do this:

$('.Find').each(function() {
    var $this = $(this);
    $this.autocomplete('auto/data.php?mode='+ $this.attr('id')  + '', {    
        width: 220,
        mzx: 5,
        selectFirst: false,      
        mustMatch:true,
        formatItem: function(data, i, n, value) {return  value.split("|")[0];}
    });
});

      

$('.Find').attr('id')

will always return the id of the first one .Find

on the page. Therefore, you cannot use it like this.

+1


source


$('.Find')

returns an array of all DOM elements (wrapped in jquery) that got the Find class.

You can access each element using the [] operator - for example, $('.Find')[0]

or for

loop over the array using lop. Use the id property to get the id of an element.

To summarize - $('.Find')[0].id

- the id of the first element

+1


source


I'm not sure if I'm getting what you want, but I'm assuming you want to autocomplete all inputs with data from different urls based on the input, if you can:

$('.Find').each(function(){
  $(this).autocomplete('auto/data.php?mode='+ $(this).attr('id')  +'', {    
                width: 220,
                max: 5,
                selectFirst: false,      
                mustMatch:true,
                formatItem: function(data, i, n, value) {return  value.split("|")[0];}
  });
})

      

to get the id, in each just do:

$(this).attr('id') or $(this).prop('id')

      

+1


source


To have a dynamic data source you need to set function()

as source.

Also, the attribute this

does not return the actual DOM element , not a richer object that (of course) contains the actual element.

So, to achieve what you want, you can try the following:

$('.Find').autocomplete({    
    source: function(request, response) {
        var id = this.element[0].id;
        var url = 'auto/data.php?mode='+ id;
        jQuery.getJSON(url, response).error(function () { /* handle JSON error */ }); 
    },
    width: 220,
    max: 5,
    selectFirst: false,      
    mustMatch:true,
    formatItem: function(data, i, n, value) {return  value.split("|")[0];}
});

      

+1


source







All Articles