0

I want to write a custom js plugin which needs to get initiated if there is a data attribute included in my HTML:

<input value="98" data-my-plugin="number">

If data-my-plugin is included, I want to initiate my js or jquery plugin automatically.

1
  • 1
    Did you try to write some code? Commented Aug 13, 2016 at 4:20

1 Answer 1

2

You can use .ready(), attribute selector "[data-my-plugin]" or "[data-my-plugin=number]"

(function($) {
  $.fn.extend({
    "myPlugin": function() {
      // do stuff
      return this.val()
    }
  });
}(jQuery));

$(document).ready(function() {
  console.log($("[data-my-plugin]").myPlugin());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<input value="98" data-my-plugin="number">

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.