hasAttributes() method in javascript
hasAttributes() method is used to check and return whether an element has any type of attributes or not.
This method returns Boolean value.
Example Explanation
- In the above example we have created a button (idBtn) with attributes id, onclick and a Javascript function is declared for it (fun_name();).
- When the button is clicked Javascript function is called.
- In the Javascript function, first we are getting the button element(getElementById("idBtn");) and storing in elem variable.
- In the next step we are using the if.. else statement.
- In the if statement we are checking if the elem variable has any attributes in it.
- Since two attributes are present, it returns true and the alert statement inside if statement is executed (alert("Given attribute is present");).