hamburger
hamburger

Reputation: 1435

how to pass an element to a function

I would like to pass the form.find element to my function AvailabilityChecker. I tried to get the elements Attribute with no success. Whats wrong?

var AvailabilityChecker = function (ev, el) {   
       console.log("arguments: ",Array.prototype.slice.call(arguments)); //gives: arguments:  Array [ Object ]
       console.log(ev);
       console.log("AvailabilityChecker: ",el.attr('AvailabilityChecker') //gives Typeerror
}


form.find("input[AvailabilityChecker]").keyup(AvailabilityChecker.bind(this));

Upvotes: 0

Views: 43

Answers (1)

epascarello
epascarello

Reputation: 207557

The el is not going to be an attribute magically appended as an argument. So you need to do it another way. And using .bind() is also changes how you can get the element reference

var AvailabilityChecker = function (ev) {  
  console.log("1-DOM", ev.target.getAttribute("AvailabilityChecker"))
  console.log("1-JQ",$(ev.target).attr("AvailabilityChecker"))
}

$("input[AvailabilityChecker]").keyup(AvailabilityChecker.bind(this));

var AvailabilityChecker2 = function (ev) {  
  console.log("2-DOM", this.getAttribute("AvailabilityChecker"))
  console.log("2-JQ",$(this).attr("AvailabilityChecker"))
}

$("input[AvailabilityChecker]").keyup(AvailabilityChecker2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input AvailabilityChecker="aaaa">

Upvotes: 2

Related Questions