user125264
user125264

Reputation: 1827

getting the ID of this in a plugin in jquery

I have something i have been trying to figure out for for some time now.

Im using this as an example. the pickadate.js date picker. I load the plugin up and i bind it to all my inputs with a class of datepicker.

Because I have it bound to all inputs with a class of datepicker.

Im wondering how do i go about using the this scope within the events that can be triggered?

Say i click on date1 and its bound to the jquery plugin, is the this keyword available to me to extract the id via its events within the plugin ? I know that all plugins can be different, but im hoping for an easy way to get the id as part of a generic function each time i use a plugin.

is there a simple way ? as this always returns undefined

$('input.datepicker').pickadate({
    clear : 'Clear',
    format : 'dd/mm/yyyy',
    clear: 'Clear selection',
    labelMonthNext: 'Go to the next month',
    labelMonthPrev: 'Go to the previous month',
    labelMonthSelect: 'Pick a month from the dropdown',
    labelYearSelect: 'Pick a year from the dropdown',
    editable : false,
    disable: [
        true,
        6  
    ],
    onOpen : function(){console.log($(this).attr('id'));}
});

Upvotes: 1

Views: 266

Answers (1)

Stevangelista
Stevangelista

Reputation: 1829

According to the docs for Pickadate, the context of this within the scope of the available Events is the actual "picker" object and not the HTML <input> that Pickadate is bound to: http://amsul.ca/pickadate.js/date/#events

As such, you want to use their API - and specifically the .get('id') method to return the triggering/bound element's ID: http://amsul.ca/pickadate.js/api/#method-get-id

Example:

onOpen : function(){ 
    alert('The ID of the input you clicked is: "' + this.get('id') + '"');
}

jsFiddle: http://jsfiddle.net/LM2xb/10/

Upvotes: 2

Related Questions