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.
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">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.