You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>As of jQuery 1.4, the second argument to <code>jQuery()</code> can accept a plain object consisting of a superset of the properties that can be passed to the <ahref="/attr">.attr()</a> method. Furthermore, any <ahref="/category/events/">event type</a> can be passed in, and the following jQuery methods can be called: <ahref="/val">val</a>, <ahref="/css">css</a>, <ahref="/html">html</a>, <ahref="/text">text</a>, <ahref="/data">data</a>, <ahref="/width">width</a>, <ahref="/height">height</a>, or <ahref="/offset">offset</a>.</p>
177
+
<p>As of jQuery 1.4, the second argument to <code>jQuery()</code> can accept a plain object consisting of a superset of the properties that can be passed to the <ahref="/attr">.attr()</a> method.</p>
178
+
<p><strong>Important:</strong> If the second argument is passed, the HTML string in the first argument must represent a a simple element with no attributes. <strong>As of jQuery 1.4</strong>, any <ahref="/category/events/">event type</a> can be passed in, and the following jQuery methods can be called: <ahref="/val">val</a>, <ahref="/css">css</a>, <ahref="/html">html</a>, <ahref="/text">text</a>, <ahref="/data">data</a>, <ahref="/width">width</a>, <ahref="/height">height</a>, or <ahref="/offset">offset</a>.</p>
178
179
<p><strong>As of jQuery 1.8</strong>, any jQuery instance method (a method of <code>jQuery.fn</code>) can be used as a property of the object passed to the second parameter:</p>
179
180
<pre><code>
180
181
$( "<div></div>", {
@@ -187,6 +188,16 @@ $( "<div></div>", {
187
188
}).appendTo( "body" );
188
189
</code></pre>
189
190
<p>The name <code>"class"</code> must be quoted in the object since it is a JavaScript reserved word, and <code>"className"</code> cannot be used since it refers to the DOM property, not the attribute. </p>
191
+
<p>While the second argument is convenient, its flexibility can lead to unintended consequences (e.g. <code>$("<input>", {size: "4"})</code> calling the <code>.size()</code> method instead of setting the size attribute). The previous code block could thus be written instead as:</p>
192
+
<pre><code>
193
+
$( "<div></div>" )
194
+
.addClass( "my-div" )
195
+
.on({
196
+
touchstart: function( event ) {
197
+
// do something
198
+
}
199
+
}).appendTo( "body" );
200
+
</code></pre>
190
201
</longdesc>
191
202
<example>
192
203
<desc>Create a div element (and all of its contents) dynamically and append it to the body element. Internally, an element is created and its innerHTML property set to the given markup.</desc>
0 commit comments