In this short post, I will show you how to add/remove attributes or properties to DOM elements and how to add/remove CSS classes from DOM elements using jQuery. Also, howattribute and properties are different in jQuery.
Before jQuery 1.6 there was no concept of property. Everything was treated as attributes. But now you must be wondering about how attribute and property are different in jQuery. Find out answer at below post,
Add Property: The general syntax to add attribute to one or set of DOM elements
Add Multiple Properties: You can also add multiple properties together using prop().
Remove Property:
Note: prop and removeProp are available with jQuery 1.6 and its higher version.
Add Attribute: The general syntax to add attribute to one or set of DOM elements
Add Multiple Attributes: You can also add multiple attribute together using attr().
Remove Attribute:
Add CSS Class: The general syntax to add css class to one or set of DOM elements
Add Multiple Classes: You can also add multiple CSS classes together using addClass().
Remove CSS Class:
Remove All CSS Class: To remove all associated CSS classes from element, then don't pass the class name while making a call to removeClass() method.
Element has the cssClass associated with it:
Feel free to contact me for any help related to jQuery, I will gladly help you.
Before jQuery 1.6 there was no concept of property. Everything was treated as attributes. But now you must be wondering about how attribute and property are different in jQuery. Find out answer at below post,
Properties:
Add Property: The general syntax to add attribute to one or set of DOM elements
1 | $( "selector" ).prop( "propertyname" , "value" ); |
1 | $( "selector" ).prop({property1: "value" , property2: "value" }) |
1 | $( "selector" ).removeProp( "PropertyName" ); |
Attributes:
Add Attribute: The general syntax to add attribute to one or set of DOM elements
1 | $( "selector" ).attr( "attributeName" , "value" ); |
1 | $( "selector" ).attr({attribute1: "value" , attribute2: "value" }) |
1 | $( "selector" ).removeAttr( "attributeName" ); |
CSS Classes:
Add CSS Class: The general syntax to add css class to one or set of DOM elements
1 | $( "selector" ).addClass( "classname" ); |
1 | $( "selector" ).addClass( 'class1 class2' ); |
1 | $( "selector" ).removeClass( "classname" ); |
1 | $( "selector" ).removeClass(); |
1 | $( "selector" ).hasClass( "classname" ); |
0 comments:
Post a Comment
Don't Forget to comment