setProperty method
9 | ||||
Adds a property with the specified name and value to the current style object.
Note: The setProperty method is supported in Internet Explorer from version 9.
If a property with the same name exists on the current style object, then it modifies its value.
In older Internet Explorer versions (and in newer ones as well), the setAttribute method provides similar functionality.
Another possibility to set the value of a CSS property is to use the corresponding JavaScript property.
See the examples below for details.
- To get the value of a style property, use the getPropertyValue and getAttribute methods.
- To remove a style property, use the removeProperty and removeAttribute methods.
Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
Required. String that specifies the name of the style property. This parameter is not case sensitive. | |||||||||||||||||||||||
Required. String that specifies the value for the style property. | |||||||||||||||||||||||
Required. String that specifies the priority of the style property. The priority of a style property can be retrieved by the getPropertyPriority method.
The priority can be one of the following values:
|
Return value:
This method has no return value.
Example HTML code 1:
This example illustrates the use of the setProperty method:
|
||||
<head> <script type="text/javascript"> function ModifyBGColor (button) { if (button.style.setProperty) { button.style.setProperty ("background-color", "green", null); } else { button.style.setAttribute ("backgroundColor", "green"); } } </script> </head> <body> <button onclick="ModifyBGColor (this);" style="background-color:red;">Modify my background color!</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example is equivalent to the previous one, but it uses the backgroundColor style property to modify the background color of an element:
|
||||
<head> <script type="text/javascript"> function ModifyBGColor (button) { button.style.backgroundColor = "green"; } </script> </head> <body> <button onclick="ModifyBGColor (this);" style="background-color:red;">Modify my background color!</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
CSSStyleDeclaration, htmlElement.currentStyle, htmlElement.style
HTML elements:
a, abbr, acronym, address, applet, area, b, base, basefont, bdo, bgsound, big, blink, blockquote, body, br, button, caption, center, cite, code, col, colgroup, comment, dd, del, dfn, dir, div, dl, dt, em, embed, fieldset, font, form, frame, frameset, h1, h2, h3, h4, h5, h6, head, hr, html, i, iframe, img, input:button, input:checkbox, input:file, input:hidden, input:image, input:password, input:radio, input:range, input:reset, input:search, input:submit, input:text, ins, isindex, kbd, keygen, label, legend, li, link, listing, map, marquee, menu, meta, nobr, noframes, noscript, object, ol, optgroup, option, p, param, plaintext, pre, q, rt, ruby, s, samp, script, select, small, span, strike, strong, style, sub, sup, table, tbody, td, textarea, tfoot, th, thead, title, tr, tt, u, ul, var, wbr, xml, xmp
Related pages:
External links:
User Contributed Comments