{"id":1905,"date":"2017-03-23T18:49:40","date_gmt":"2017-03-23T13:19:40","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1905"},"modified":"2017-03-28T17:46:52","modified_gmt":"2017-03-28T12:16:52","slug":"prop-vs-attr","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/prop-vs-attr\/","title":{"rendered":"JAVASCRIPT &#8211; .prop() vs .attr()"},"content":{"rendered":"<h4 id=\"attr\"><span style=\"color: #ff0000;\"><b>.attr()<\/b><\/span><\/h4>\n<p>Get the value of an attribute for the first element in the set of matched elements.<\/p>\n<h4 id=\"prop\"><span style=\"color: #800080;\"><b>.prop()<\/b><\/span><\/h4>\n<p>Get the value of a property for the first element in the set of matched elements.<\/p>\n<h4 id=\"what-is-attributes\"><span style=\"color: #ff6600;\"><b>What is Attributes?<\/b><\/span><\/h4>\n<ul>\n<li>Attributes carry additional information about an HTML element and come in <b>name=\u201dvalue\u201d <\/b>pairs. we can set an attribute for HTML element.<\/li>\n<\/ul>\n<h4 id=\"example\"><span style=\"color: #000000;\">Example :<\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">&lt;input id=\u201cwiki&quot; type=\u201cwiki&quot; value=\u201cwiki&quot;&gt;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<ul>\n<li>here, &#8220;type&#8221;,&#8221;value&#8221;, &#8220;id&#8221; are attributes of the input elements.<\/li>\n<\/ul>\n<h4 id=\"what-is-property\"><span style=\"color: #000000;\"><b>What is Property? <\/b><\/span><\/h4>\n<ul>\n<li><span style=\"color: #000000;\">Property is a representation of an attribute in the HTML DOM.<\/span><\/li>\n<li><span style=\"color: #000000;\">once the browser parse our HTML code ,corresponding DOM node will be created which is an object thus having properties.<\/span><\/li>\n<li><span style=\"color: #000000;\">since, attr() gives the value of element as it was defines in the html on page load.<\/span><\/li>\n<li><span style=\"color: #000000;\">It is always recommended to use prop() to get values of elements which is modified through javascript\/jquery , as it gives the original value of an element&#8217;s current state.<\/span><\/li>\n<\/ul>\n<h4 id=\"attr-vs-prop\"><span style=\"color: #000000;\"><b>.attr vs .prop:<\/b><\/span><\/h4>\n<h4 id=\"style\"><span style=\"color: #000000;\"><b>style<\/b><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">&lt;input style=&quot;font:arial;&quot;\/&gt;<br\/>.attr(&#039;style&#039;) \u2013 It returns inline styles for the matched element i.e. &quot;font:arial;&quot;<br\/>.prop(&#039;style&#039;) \u2013 It returns an style declaration object i.e. CSSStyleDeclaration<\/code><\/pre> <\/div>\n<h4 id=\"value\"><span style=\"color: #000000;\"><b>value<\/b><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">&lt;input value=&quot;wikitechy&quot; type=&quot;text&quot;\/&gt;   <br\/>$(&#039;input&#039;).prop(&#039;value&#039;, &#039;i changed the value&#039;);<br\/><br\/>.attr(&#039;value&#039;) -- returns &#039;wikitechy&#039; *<br\/>.prop(&#039;value&#039;) -- returns &#039;i changed the value&#039;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"note\"><span style=\"color: #000000;\"><b>Note: <\/b><\/span><\/h4>\n<p>jQuery for this reason has a .val() method, which internally is equivalent to .prop(&#8216;value&#8217;)<\/p>\n<h4 id=\"what-is-the-difference-between-prop-and-attr\"><span style=\"color: #000000;\"><b>What is the difference between .prop() and .attr():<\/b><\/span><\/h4>\n<table style=\"height: 252px;\" width=\"814\">\n<tbody>\n<tr>\n<td width=\"524\"><b>.prop() <\/b><\/td>\n<td width=\"524\"><b>.attr()<\/b><\/td>\n<\/tr>\n<tr>\n<td width=\"524\">A\u00a0property\u00a0is the current state of the input element<\/td>\n<td width=\"524\">An\u00a0<i>attribute<\/i>\u00a0is the default value.<\/td>\n<\/tr>\n<tr>\n<td width=\"524\">\u00a0jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() only retrieves attributes.<\/td>\n<td width=\"524\">jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior.<\/td>\n<\/tr>\n<tr>\n<td width=\"524\"><b>properties<\/b>\u00a0are in HTML\u00a0<i>DOM tree<\/i>\u00a0(== basically an actual property of some object in JS).<\/td>\n<td width=\"524\"><b>attributes<\/b>\u00a0are in\u00a0 HTML\u00a0<i>text document\/file<\/i>\u00a0(== imagine this is the result of our html markup parsed),<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<table style=\"height: 301px;\" width=\"814\">\n<tbody>\n<tr>\n<td width=\"557\"><b>.prop() <\/b><\/td>\n<td width=\"557\"><b>.attr()<\/b><\/td>\n<\/tr>\n<tr>\n<td width=\"557\">.prop()\u00a0changes properties for that HTML tag as per the DOM tree.<\/td>\n<td width=\"557\">.attr()\u00a0changes attributes for that HTML tag.<\/td>\n<\/tr>\n<tr>\n<td width=\"557\">.prop()\u00a0can return any type &#8211; string, integer, Boolean.<\/td>\n<td width=\"557\">.attr()\u00a0always returns a string<\/td>\n<\/tr>\n<tr>\n<td width=\"557\">When we are checking Image <b>src<\/b> property. If the src is empty, prop return the current url of the page.<\/td>\n<td width=\"557\">When we are checking Image <b>src<\/b> property. If the src is empty, attr return empty string.<\/td>\n<\/tr>\n<tr>\n<td width=\"557\">\u00a0.prop() was added later to be more semantic and it works better with value-less attributes like &#8216;checked&#8217; and &#8216;selected&#8217;.<\/td>\n<td width=\"557\">.attr() is used to get attributes from the HTML tags, and while it is perfectly functional.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<table style=\"height: 328px;\" width=\"816\">\n<tbody>\n<tr>\n<td width=\"485\"><b>.prop() <\/b><\/td>\n<td width=\"485\"><b>.attr()<\/b><\/td>\n<\/tr>\n<tr>\n<td width=\"485\">Get the value of a\u00a0<b>property<\/b>\u00a0for the first element in the set of matched elements.<\/p>\n<p>To gives the updated values of elements which is modified via javascript \/ jquery<\/td>\n<td width=\"485\">Get the value of an\u00a0<b>attribute<\/b>\u00a0for the first element in the set of matched elements.<\/p>\n<p>To gives the value of element as it was defines in the html on page load<\/td>\n<\/tr>\n<tr>\n<td width=\"485\">Getting a custom HTML attribute (since it&#8217;s not synced with a DOM property).<\/td>\n<td width=\"485\">Getting a HTML attribute that doesn&#8217;t sync with a DOM property, e.g. get the &#8220;original value&#8221; of a standard HTML attribute, like\u00a0&lt;input value=\u201cwiki&#8221;&gt;.<\/td>\n<\/tr>\n<tr>\n<td width=\"485\">\u00a0<b>Purpose: <\/b>Gives access to properties that belong to element nodes. These properties are similar to attributes, but are only accessible through JavaScript.<\/td>\n<td width=\"485\"><b>Purpose:<\/b>\u00a0Allows markup to have data associated with it for events, rendering, and other purposes.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n[ad type=&#8221;banner&#8221;]\n<ul>\n<li>Generally, we need prop() rather than attr().<\/li>\n<li>Replacing calls to attr() with prop() in our code will generally work.<\/li>\n<li>Properties are generally simpler to deal with than attributes.<\/li>\n<li>An attribute value may only be a string whereas a property can be of any type.<\/li>\n<li>For example, the checked property is a Boolean, the style property is an object with individual properties for each style, the size property is a number.<\/li>\n<li>Both a property and an attribute with the same name exists, usually updating one will update the other, but this is not the case for certain attributes of inputs, such as value and checked: for these attributes, the property always represents the current state while the attribute (except in old versions of IE) corresponds to the default value of the input (reflected in the default Value \/ default Checked property).<\/li>\n<li>As an example of how properties are simpler to deal with than attributes, consider a checkbox that is initially checked. Here are two possible valid HTML :<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">&lt;input id=&quot;cb&quot; type=&quot;checkbox&quot; checked&gt;<br\/>&lt;input id=&quot;cb&quot; type=&quot;checkbox&quot; checked=&quot;checked&quot;&gt;<\/code><\/pre> <\/div>\n<p>So, how to find out if the checkbox is checked with jQuery:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">if ( $(&quot;#cb&quot;).attr(&quot;checked&quot;) === true ) {...}<br\/>if ( $(&quot;#cb&quot;).attr(&quot;checked&quot;) == &quot;checked&quot; ) {...}<br\/>if ( $(&quot;#cb&quot;).is(&quot;:checked&quot;) ) {...}<\/code><\/pre> <\/div>\n<ul>\n<li>This is actually the simplest thing in the world to do with the checked Boolean property.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">if (document.getElementById(&quot;cb&quot;).checked) {...}<\/code><\/pre> <\/div>\n<ul>\n<li>The property also makes checking or unchecking the checkbox trivial:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">document.getElementById(&quot;cb&quot;).checked = false<\/code><\/pre> <\/div>\n<ul>\n<li>In jQuery 1.6,<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">$(&quot;#cb&quot;).prop(&quot;checked&quot;, false)<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>The idea of using the checked attribute for scripting a checkbox is unnecessary.<\/p>\n<ol>\n<li>It&#8217;s not obvious what the correct way to check or uncheck the checkbox is using the checked attribute<\/li>\n<li>The attribute value reflects the default rather than the current visible state (except in some older versions of IE, thus making things still harder). The attribute defines whether the checkbox on the page is checked.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>.attr() Get the value of an attribute for the first element in the set of matched elements. .prop() Get the value of a property for the first element in the set of matched elements. What is Attributes? Attributes carry additional information about an HTML element and come in name=\u201dvalue\u201d pairs. we can set an attribute [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[275,1208],"tags":[4108,4105,4109,4110,4111,4104,4116,4103,4113,4106,4121,4114,4117,4115,4112,4118,4119,4120,4107],"class_list":["post-1905","post","type-post","status-publish","format-standard","hentry","category-javascript","category-jquery","tag-attr","tag-prop","tag-and-val","tag-difference-between-prop-and-attr","tag-difference-between-prop-and-attr-in-jquery","tag-difference-between-prop-and-attr-in-jquery-and-when-to-use-attribute","tag-html-property-vs-attribute","tag-javascript-prop-vs-attr","tag-jquery-attr-vs-prop","tag-jquery-attr-vs-prop-difference","tag-jquery-check-if-checkbox-is-checked","tag-jquery-prop-disabled","tag-jquery-prop-selected","tag-jquery-prop-vs-attr","tag-jquery-prop-method","tag-jquery-remove-property","tag-jquery-set-checkbox-checked","tag-jquery-uncheck-checkbox","tag-what-are-the-differences-between-prop"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1905"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1905\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}