As the title suggests, what’s the difference(s)?
I guess that the latter one can do what the former can at least.
Is my opinion right?
P.S: obj is a DOM Element
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.
setAttributeis a DOM Element method — it can only be used on DOM elements. If you try to use it on your own objects aTypeErrorwill be thrown as they do not support that method unless you create it yourself. For example:However, the second method you have described is a valid way to assign properties and methods to your own objects and DOM objects (and there are a few special ones baked in for DOM objects in most browsers, such as onclick, like you’ve observed).
You can read more about the Element interface and supported methods here: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-745549614 (scroll down a bit for setAttribute)