5
<title>Example: Set and Get Properties</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: Set and Get Properties</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><link href="../assets/node/node.css" rel="stylesheet" type="text/css">
22
<p>This example demonstrates how to retrieve and use a <code>Node</code> instance and access DOM properties.</p>
23
<p>Press the button to get or set the <code>FORM</code>'s <code>offsetWidth</code>.</p>
27
<form id="demo" action="#">
29
<input name="width" size="2">
30
<input type="submit" value="Run">
33
<script type="text/javascript">
34
YUI().use('node', function(Y) {
35
var form = Y.one('#demo'),
36
input = Y.one('#demo input[name=width]'),
37
button = Y.one('#demo input[type=submit]');
39
form.on('submit', function(e) {
40
e.preventDefault(); // keep the form from submitting
42
var value = input.get('value'),
43
width = form.get('offsetWidth');
46
input.set('value', width);
47
} else if (!isNaN(parseInt(value))) {
48
form.set('offsetWidth', value);
56
<h2>Setting up the HTML</h2>
58
<pre class="code prettyprint"><form id="demo" action="#">
59
<label>How wide is this form?</label>
60
<input name="width" size="2">
61
<input type="submit" value="Show Me">
62
</form></pre>
65
<h2>Getting a Node Instance</h2>
66
<p>The <code>Y.one</code> method accepts a Selector or HTML element and returns a Node instance. Let's go ahead and set up some variables for the node's representing our HTML.</p>
68
<pre class="code prettyprint">var form = Y.one('#demo'),
69
input = Y.one('#demo input[name=width]'),
70
button = Y.one('#demo input[type=submit]');</pre>
73
<h2>Accessing Node Properties</h2>
74
<p>The properties of a node can be accessed via its <code>set</code> and <code>get</code> methods.</p>
75
<p>In most cases, simple type of properties (strings, numbers, booleans) pass directly to/from the underlying DOM node, however properties representing other DOM nodes return <code>Node</code> or <code>NodeList</code> instances.</p>
77
<h3>The Get Method</h3>
78
<p>We can use the <code>get</code> method to read the <code>FORM</code> element's offsetWidth, which includes the style width, padding, and border.</p>
80
<pre class="code prettyprint">width = form.get('offsetWidth');</pre>
83
<h3>The Set Method</h3>
84
<p>The <code>set</code> method allows us to update the value of the <code>INPUT</code> element with the value of the <code>FORM</code>'s <code>offsetWidth</code> property.</p>
86
<pre class="code prettyprint">input.set('value', width);</pre>
89
<h3>Setting the Offset Width</h3>
90
<p>The <code>offsetWidth</code> property of an HTML element is read only, however YUI makes this writeable as well. This assures that the final <code>offsetWidth</code> matches the value that is set, regardless of borders, padding, or box model.</p>
92
<pre class="code prettyprint">form.set('offsetWidth', value);</pre>
95
<h2>Listening for Node Events</h2>
96
<p>We will update the value of the <code>INPUT</code> when the <code>BUTTON</code> is pressed. The <code>FORM</code>'s default action can be suppressed using the incoming Event object's <code>preventDefault()</code> method.</p>
98
<pre class="code prettyprint">button.on('click', function(e) {
103
<h2>Complete Example Source</h2>
104
<pre class="code prettyprint"><form id="demo" action="#">
105
<label>Width:</label>
106
<input name="width" size="2">
107
<input type="submit" value="Run">
110
<script type="text/javascript">
111
YUI().use('node', function(Y) {
112
var form = Y.one('#demo'),
113
input = Y.one('#demo input[name=width]'),
114
button = Y.one('#demo input[type=submit]');
116
form.on('submit', function(e) {
117
e.preventDefault(); // keep the form from submitting
119
var value = input.get('value'),
120
width = form.get('offsetWidth');
122
if (value == '') {
123
input.set('value', width);
124
} else if (!isNaN(parseInt(value))) {
125
form.set('offsetWidth', value);
129
</script></pre>
134
<div id="sidebar" class="yui3-u">
138
<div class="sidebox">
140
<h2 class="no-toc">Examples</h2>
144
<ul class="examples">
147
<li data-description="Using selectors and property accessors with Node.">
148
<a href="properties.html">Set and Get Properties</a>
153
<li data-description="Using DOM methods with Node.">
154
<a href="dom-node.html">DOM Methods</a>
159
<li data-description="Listening for DOM events with Node instances.">
160
<a href="events.html">Handling DOM Events</a>
165
<li data-description="NodeList provides Node functionality for multiple nodes.">
166
<a href="nodelist.html">NodeList</a>
171
<li data-description="Using a single event listener to handle events on multiple nodes.">
172
<a href="node-evt-delegation.html">Delegating Node Events</a>
177
<li data-description="This example demonstrates how to position an element in page coordinates.">
178
<a href="node-xy.html">Node Positioning</a>
183
<li data-description="This example demonstrates how to set styles and get style information.">
184
<a href="node-style.html">Node Styling</a>
189
<li data-description="This example demonstrates how to insert content into a Node.">
190
<a href="node-insert.html">Adding Node Content</a>
195
<li data-description="This example demonstrates how to show and hide a Node.">
196
<a href="node-view.html">Showing and Hiding</a>
220
<div class="sidebox">
222
<h2 class="no-toc">Examples That Use This Component</h2>
226
<ul class="examples">
247
<li data-description="Use the Event Utility to attach simple DOM event handlers.">
248
<a href="../event/basic-example.html">Simple DOM Events</a>
253
<li data-description="Creating an accessible toolbar using the Focus Manager Node Plugin and Node's support for the WAI-ARIA Roles and States.">
254
<a href="../node-focusmanager/node-focusmanager-1.html">Accessible Toolbar</a>
259
<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.">
260
<a href="../node-focusmanager/node-focusmanager-3.html">Accessible Menu Button</a>
265
<li data-description="Use IO to request XML data from a remote web service.">
266
<a href="../io/weather.html">Request XML data from Yahoo! Weather</a>
271
<li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources.">
272
<a href="../io/xdr.html">Request JSON using Yahoo! Pipes</a>
277
<li data-description="Example Photo Browser application.">
278
<a href="../dd/photo-browser.html">Photo Browser</a>
283
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation.">
284
<a href="../dd/portal-drag.html">Portal Style Example</a>
296
<script src="../assets/vendor/prettify/prettify-min.js"></script>
297
<script>prettyPrint();</script>