5
<title>Example: Set and Get Properties</title>
6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
15
<h1>Example: Set and Get Properties</h1>
20
<div class="yui3-u-3-4">
22
<div class="content"><link href="../assets/node/node.css" rel="stylesheet" type="text/css">
24
<p>This example demonstrates how to retrieve and use a <code>Node</code> instance
25
and access DOM properties.</p>
26
<p>Press a button to get or set the <code>offsetWidth</code> of the <code>div</code>
27
containing the corn image.</p>
34
background: url(../assets/node/images/corn.jpg);
37
-moz-box-shadow: 3px 3px 12px rgba(0, 0, 0, 0.4);
38
-webkit-box-shadow: 3px 3px 12px rgba(0, 0, 0, 0.4);
45
background: url("../assets/node/images/ruler_ticks.png") repeat-x scroll -1px 24px #DDCEB7;
47
.example .yui3-button {
71
border-right: solid 1px #f00;
76
<div id="ruler"></div>
78
<div class="ruler-marker"></div>
79
<div id="output">? px</div>
82
<input id="input" size="2" value="550"> px
83
<button class="yui3-button btn-set">Set</button>
84
<button class="yui3-button btn-get">Get</button>
86
YUI().use('node', 'button', function(Y) {
87
var corn = Y.one('#corn'),
88
input = Y.one('.example #input'),
89
output = Y.one('.example #output');
91
var getWidth = function(){
92
var width = corn.get('offsetWidth');
93
output.setContent(width + 'px'); // display width near the get button
96
Y.one('.example .btn-get').on('click', getWidth);
97
output.on('click', getWidth); // also allows getting width by clicking on ruler width label
99
Y.one('.example .btn-set').on('click', function(e) {
100
var value = input.get('value'),
101
width = corn.get('offsetWidth');
103
input.set('value', width);
104
} else if (!isNaN(parseInt(value))) { // if the value in the input is a number
105
corn.set('offsetWidth', value);
106
output.setContent('? ' + 'px'); // clear out the width label on the ruler
111
<div style="color:#aaa; display:block; font-size:90%; margin-top:1em;">Image derived from
112
<a href="http://www.flickr.com/photos/29278394@N00/3934433763/" target="_blank">
113
"ear of corn"</a>, by normanack</div>
116
<h2>Setting up the HTML</h2>
118
<pre class="code prettyprint"><div id="ruler"></div>
119
<div id="corn">
120
<div class="ruler-marker"></div>
121
<div id="output">? px</div>
123
<label>Width:</label>
124
<input id="input" size="2" value="550"> px
125
<button class="yui3-button btn-set">Set</button>
126
<button class="yui3-button btn-get">Get</button></pre>
129
<h2>Getting a Node Instance</h2>
130
<p>The <code>Y.one</code> method accepts a Selector or HTML element and
131
returns a Node instance. First we'll set up some variables
132
for the node's representing our HTML.</p>
134
<pre class="code prettyprint">var corn = Y.one('#corn'),
135
input = Y.one('.example #input'),
136
output = Y.one('.example #output');</pre>
139
<h2>Accessing Node Properties</h2>
140
<p>The properties of a node can be accessed via its <code>set</code> and
141
<code>get</code> methods.</p>
142
<p>In most cases, simple type of properties (strings, numbers, Booleans)
143
pass directly to/from the underlying DOM node, however properties representing
144
other DOM nodes return <code>Node</code> or <code>NodeList</code> instances.</p>
146
<h3>The Get Method</h3>
147
<p>We can use the <code>get</code> method to read the <code>offsetWidth</code> of the <code>div</code>
148
containing the corn image,
149
which includes the style width, padding, and border.</p>
151
<pre class="code prettyprint">width = corn.get('offsetWidth');</pre>
154
<h3>The Set Method</h3>
155
<p>The Set method can be used to set the value of objects
157
<pre class="code prettyprint">input.set('value', 237);</pre>
160
<p>The <code>offsetWidth</code> property of an HTML element is read only, however YUI
161
makes this writeable. This assures that the final <code>offsetWidth</code>
162
matches the value that is set, regardless of borders, padding, or box model.</p>
164
<pre class="code prettyprint">corn.set('offsetWidth', value);</pre>
167
<h2>Listening for Node Events</h2>
168
<p>We will update the value <code>offsetWidth</code> property of the <code>div</code>
169
containing the corn image when the Set button is pressed.
172
<pre class="code prettyprint">Y.one('.example .btn-set').on('click', function(e) {
174
corn.set('offsetWidth', value);
179
<h2>Complete Example Source</h2>
180
<pre class="code prettyprint"><style>
183
background: url(../assets/node/images/corn.jpg);
186
-moz-box-shadow: 3px 3px 12px rgba(0, 0, 0, 0.4);
187
-webkit-box-shadow: 3px 3px 12px rgba(0, 0, 0, 0.4);
194
background: url("../assets/node/images/ruler_ticks.png") repeat-x scroll -1px 24px #DDCEB7;
196
.example .yui3-button {
220
border-right: solid 1px #f00;
225
<div id="ruler"></div>
226
<div id="corn">
227
<div class="ruler-marker"></div>
228
<div id="output">? px</div>
230
<label>Width:</label>
231
<input id="input" size="2" value="550"> px
232
<button class="yui3-button btn-set">Set</button>
233
<button class="yui3-button btn-get">Get</button>
235
YUI().use('node', 'button', function(Y) {
236
var corn = Y.one('#corn'),
237
input = Y.one('.example #input'),
238
output = Y.one('.example #output');
240
var getWidth = function(){
241
var width = corn.get('offsetWidth');
242
output.setContent(width + 'px'); // display width near the get button
245
Y.one('.example .btn-get').on('click', getWidth);
246
output.on('click', getWidth); // also allows getting width by clicking on ruler width label
248
Y.one('.example .btn-set').on('click', function(e) {
249
var value = input.get('value'),
250
width = corn.get('offsetWidth');
251
if (value == '') {
252
input.set('value', width);
253
} else if (!isNaN(parseInt(value))) { // if the value in the input is a number
254
corn.set('offsetWidth', value);
255
output.setContent('? ' + 'px'); // clear out the width label on the ruler
259
</script></pre>
265
<div class="yui3-u-1-4">
266
<div class="sidebar">
270
<div class="sidebox">
272
<h2 class="no-toc">Examples</h2>
276
<ul class="examples">
279
<li data-description="Using selectors and property accessors with Node.">
280
<a href="properties.html">Set and Get Properties</a>
285
<li data-description="Using DOM methods with Node.">
286
<a href="dom-node.html">DOM Methods</a>
291
<li data-description="Listening for DOM events with Node instances.">
292
<a href="events.html">Handling DOM Events</a>
297
<li data-description="NodeList provides Node functionality for manipulating multiple nodes at once.">
298
<a href="nodelist.html">Using NodeList - Simple</a>
303
<li data-description="How to use multiple NodeList features to build a simple game.">
304
<a href="ducks.html">Using NodeList - Ducks Game</a>
309
<li data-description="Using a single event listener to handle events on multiple nodes.">
310
<a href="node-evt-delegation.html">Delegating Node Events</a>
315
<li data-description="This example demonstrates how to position an element in page coordinates.">
316
<a href="node-xy.html">Node Positioning</a>
321
<li data-description="This example demonstrates how to set styles and get style information.">
322
<a href="node-style.html">Node Styling</a>
327
<li data-description="This example demonstrates how to insert content into a Node.">
328
<a href="node-insert.html">Adding Node Content</a>
333
<li data-description="This example demonstrates how to show and hide a Node.">
334
<a href="node-view.html">Showing and Hiding</a>
358
<div class="sidebox">
360
<h2 class="no-toc">Examples That Use This Component</h2>
364
<ul class="examples">
387
<li data-description="Creating an accessible toolbar using the Focus Manager Node Plugin and Node's support for the WAI-ARIA Roles and States.">
388
<a href="../node-focusmanager/node-focusmanager-1.html">Accessible Toolbar</a>
393
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States.">
394
<a href="../node-focusmanager/node-focusmanager-3.html">Accessible Menu Button</a>
399
<li data-description="Use the Event Utility to attach simple DOM event handlers.">
400
<a href="../event/basic-example.html">Simple DOM Events</a>
405
<li data-description="Use IO to request XML data from a remote web service.">
406
<a href="../io/weather.html">Request XML data from Yahoo! Weather</a>
411
<li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources.">
412
<a href="../io/xdr.html">Request JSON using Yahoo! Pipes</a>
417
<li data-description="Example Photo Browser application.">
418
<a href="../dd/photo-browser.html">Photo Browser</a>
423
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation.">
424
<a href="../dd/portal-drag.html">Portal Style Example</a>
437
<script src="../assets/vendor/prettify/prettify-min.js"></script>
438
<script>prettyPrint();</script>