var $e = $("input");
function get_html() {
return "<ul>" +
"<li>id prop: " + $e.prop("id") + "</li>" +
"<li>id attr: " + $e.attr("id") + "</li>" +
"<li>data-* prop: " + $e.prop("data-gns") + "</li>" +
"<li>data-* attr: " + $e.attr("data-gns") + "</li>" +
"<li>checked prop: " + $e.prop("checked") + "</li>" +
"<li>checked attr: " + $e.attr("checked") + "</li>" +
"</ul><br>";
};
// (1)
// id) exists in the HTML and in the DOM: it has the same interpretation
// data-*) exists only in the HTML: when the browser renders the HTML it defines it as undefined because there is not a DOM interpretation
// checked) exists in the HTML and in the DOM: it has a different interpretation
var html_ = get_html();
// (2)
$e.prop("id", "id1");
$e.attr("id", "id2");
// We force the creation of the property data-gns in the DOM, now it exists (both are different)
$e.prop("data-gns", "springfield");
$e.attr("data-gns", "vans");
// checked is special, because the attribute is deleted from HTML when it's unchecked, the best option is to use always prop
// <input checked> <!-- Checked -->
// <input checked="checked"> <!-- Checked -->
// <input> <!-- Unchecked -->
$e.prop("checked", false);
$e.attr("checked", false);
html_ = html_ + get_html();
$("body").html(html_);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.