javascript - Does 'this' refer to the element that called this function? -
in snippet below use $(this) refer element in function being called from. know not correct because printed out values , gave me 'undefined'. how refer input element?
$(function() { $( ".datepicker" ).datepicker({ onselect: function (date, obj){ if(confirm('is correct?: '+ date )) { $.post('edit.php', { "row": $(this).data('id'), "date":date, "field":$(this).name, "ajax":'true' }); } } }); }); here html element:
<input name="appvacadvp" data-id="someid" class="datepicker" size="10" type="text" placeholder="someholder" > i appreciate help. sorry being such javascript noob ._.
jquery sets this itself, point current element.
otherwise, in javascript...
as function assigned propety
var = { b: function() { // `this` `a` } } a.b(); except, property becomes assigned variable. observe...
var c = a.b; c(); // `this` point `window` as function assigned variable
var = function() { // `this` `window` } a(); as constructor
var c = function() { // `this` `c` } var c = new c(); note if forgot instantiate new, javascript assign properties global object (window in browser).
in global scope
// in global scope `this` `window` var d = this; as called call() or apply()
you can set this explicitly call() , apply() function methods.
now, problem...
thanks explanation of 'this' still dont understand why reference isn't working in code
my apologies.
$(this).name won't work this jquery object , has few properties (none of name).
either use $(this).attr('name') or drop wrapping this jquery object , access this.name.
Comments
Post a Comment