javascript - Passing variable to ON method (in jquery) -
i'm trying pass variable(present in global scope) function(present in on
method). i'm passing data object on
it's not working. it's working if take variable inside function.
here's code:
var firstname = $("#firstname").val(); $("#firstname").on("blur keyup paste", {firstname: firstname}, function(){ //access firstname })
also, best way of using on
method. should use have used above or should use this?
var firstname = $("#firstname").val(); $(document).on("blur keyup paste", "#firstname", {firstname: firstname}, function(){ //access firstname })
as event being raised on element has value you're trying retrieve can use this
reference @ it:
$("#firstname").on("blur keyup paste", function(){ var firstname = this.value; // or $(this).val(); });
with regard usage of on
, first example used when element you're attaching event exists in dom. second method when element not yet exist, may in future. should use nearest static parent element primary selector though, not document
.
Comments
Post a Comment