I’m new to js and need to extend dom Elements with my custom methods and data.
For data I’m using the dataset property built in html.
For methods I don’t know what to do.
Is there a well known best practice to achieve this ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Creating wrapper objects around DOM elements and interacting with them instead of directly with the DOM element is the best way to extend functionality of elements and is the approach taken by jQuery and many other libraries. Some libraries (notably Prototype) extended DOM prototypes directly but this is a bad idea and unreliable.
Here’s a simple example of a wrapper: