1

I am writing a general Jquery script for validation and I am stack at selecting the element for which the keypress event is fired, without actually passing the ID element #elementid specified in the below code.-->var element = **pick the object**// $('input[type=number][validate=something]');.

  • Note that the below code pickup all the input field of number type and attribute value of validation assomething.
  • get the value of maxlength of the field for which keypress event has occured.
  • avoid java script function call inside input.
  • write a general script that could be applicable for all page and not pick element by id attribute.

Sample JS below

<!DOCTYPE html>
<html>
<head>
  <title>Validation</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
        <input validate="something"  type="number" maxlength="9"  />
    <input validate="something" type="number" maxlength="9"  />

<script>
$('input[type=number][validate=something]').on('keypress', function(evt,obj) {
                var element =**pick the object**// $('input[type=number][validate=SSN]');
                var len = element.val().length + 1;
                var max = element.attr("maxlength");

                if (!(len <= max)) {
                    // some code
                }
});
</script>
</body>
</html>
yeppe
  • 679
  • 1
  • 11
  • 43
  • `var element = $(this)`. (jQuery automatically sets the function context to the element the event is being fired on. So, again `this` is your friend) – haim770 Sep 26 '16 at 07:43

1 Answers1

3

The element you've hooked the event on is available within the handler as this; to wrap it in an jQuery object, use $():

var element = $(this);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I'm not having any luck finding a dupe target for this, but surely there must be... – T.J. Crowder Sep 26 '16 at 07:48
  • @T.J.Crowder — May be [__What's the difference between $(this) and this in jQuery?__](http://stackoverflow.com/questions/3685508/whats-the-difference-between-this-and-this-in-jquery) – Rayon Sep 26 '16 at 07:51
  • @T.J.Crowder this is the dupe link .. you can duplicate it http://stackoverflow.com/q/48239/5086633 surprised by looking at the views for this question 784,472 .. my guilt feeling is gone :) – yeppe Sep 26 '16 at 11:26
  • @yeppe: Yeah, I looked at that one, but I thought the meaning was obscured by the whole `id` thing, and the most upvoted answers there focus (sometimes somewhat erroneously) on `e.target`. I think I'll probably expand the community wiki answer above a bit and start referring to this question. If you don't mind, I'll probably edit the question to be a bit more focussed as well to make it more broadly-applicable. – T.J. Crowder Sep 26 '16 at 11:53
  • @yeppe: I'll probably get to it today. Probably. It *is* on my list. – T.J. Crowder Sep 27 '16 at 06:09
  • @yeppe: If I had a link to it, I could see it, but I can't *search* for it if you follow me. It's not shown in lists. – T.J. Crowder Oct 15 '16 at 19:32
  • @yeppe: The place for that kind of question, btw, is http://meta.stackexchange.com or http://meta.stackoverflow.com. – T.J. Crowder Oct 15 '16 at 19:39