In this short post, I will show you how to add/remove attributes or properties to DOM elements and how toadd/remove CSS classes from DOM elements using jQuery. Also, how attribute 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,
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" ); |
No comments:
Post a Comment