Set and Get Style Properties
MooTools library contains different methods which are used to set or get the value of a particular style property or multiple style properties.setStyle()
This method allows you to set the value for a single property of DOM element. This method will work on the selector object of a particular DOM element. Let us take an example that provides background color for div element. Take a look at the following code.Example
<!DOCTYPE html> <html> <head> <script type = "text/javascript" src = "MooTools-Core-1.6.0.js"></script> <script type = "text/javascript" src = "MooTools-More-1.6.0.js"></script> <script type = "text/javascript"> window.addEvent('domready', function() { $('body_wrap').setStyle('background-color', '#6B8E23'); $$('.class_name').setStyle('background-color', '#FAEBD7'); }); </script> </head> <body> <div id = "body_wrap">A</div> <div class = "class_name">B</div> <div class = "class_name">C</div> <div class = "class_name">D</div> <div class = "class_name">E</div> </body> </html>You will receive the following output −
Output
getStyle()
getStyle() method is to retrieve the value of a style property of an element. Let us take an example that retrieves the background-color of a div named body_wrap. Take a look at the following syntax.Syntax
//first, set up your variable to hold the style value
var styleValue = $('body_wrap').getStyle('background-color');
Multiple Style Properties
MooTools library contains different methods used to set or get the value of a particular style property or multiple style properties.setStyle()
If you want to set multiple style properties on a single element or an array of elements then you have to use the setStyle() method. Take a look at the following syntax of the setStyle() method.Syntax
$('<element-id>').setStyles({
//use different style properties such as width, height, background-color, etc.
});
Example<!DOCTYPE html> <html> <head> <style> #body_div { width: 200px; height: 200px; background-color: #eeeeee; border: 3px solid #dd97a1; } </style> <script type = "text/javascript" src = "MooTools-Core-1.6.0.js"></script> <script type = "text/javascript" src = "MooTools-More-1.6.0.js"></script> <script type = "text/javascript"> var setWidth = function(){ $('body_div').setStyles({ 'width': 100 }); } var setHeight = function(){ $('body_div').setStyles({ 'height': 100 }); } var reset = function(){ $('body_div').setStyles({ 'width': 200, 'height': 200 }); } window.addEvent('domready', function() { $('set_width').addEvent('click', setWidth); $('set_height').addEvent('click', setHeight); $('reset').addEvent('click', reset); }); </script> </head> <body> <div id = "body_div"> </div><br/> <input type = "button" id = "set_width" value = "Set Width to 100 px"/> <input type = "button" id = "set_height" value = "Set Height to 100 px"/> <input type = "button" id = "reset" value = "Reset"/> </body> </html>You will receive the following output −
Output
Try these buttons on the web page, you can see the difference with the div size.
No comments:
Post a Comment