{"id":19913,"date":"2022-11-08T03:01:17","date_gmt":"2022-11-07T21:31:17","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-enter-password-to-display-content-of-div\/"},"modified":"2022-11-08T03:01:17","modified_gmt":"2022-11-07T21:31:17","slug":"solved-enter-password-to-display-content-of-div","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-enter-password-to-display-content-of-div\/","title":{"rendered":"[Solved] Enter password to display content of div"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-49755864\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"49755864\" data-parentid=\"49755659\" data-score=\"5\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p><strong>Because you hid the content via an <code>id<\/code> based CSS selector, adding a &#8220;show&#8221; CSS class to it later won&#8217;t override the <code>id<\/code> rule that you already set.<\/strong> (Read <strong><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/Specificity\">this<\/a><\/strong> on how different CSS selectors are more specific than others and thus, more difficult to override.)<\/p>\n<p>Here&#8217;s a quick example:<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\" data-console=\"true\" data-babel=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"snippet-code-css lang-css prettyprint-override\"><code>#d1 { display:none; }    \/* Will override most other selectors *\/\r\ndiv { display:block; }   \/* Won't work *\/\r\n.show { display:block; } \/* Won't work *\/<\/code><\/pre>\n<pre class=\"snippet-code-html lang-html prettyprint-override\"><code>&lt;p&gt;You aren't going to see the div below this paragraph even though it matches two selectors that indicate that it should be shown and even though those two selectors come after the one that hides it. This is because the way it's being hidden is with an id based selector and tag type and class selectors alone are less specific than an id selector and won't override it.&lt;\/p&gt;\r\n&lt;div id=\"d1\" class=\"show\"&gt;Hello?!&lt;\/div&gt;<\/code><\/pre>\n<\/div>\n<\/div>\n<p>So, first, set the content to be hidden with a CSS class instead of an <code>id<\/code> based selector, then later, you can just remove that class &#8211; no extra &#8220;show&#8221; class is needed.  <\/p>\n<p>Next, in your code you have a <code>div<\/code> with an <code>id<\/code> of <code>HIDDENDIV<\/code>, but your code attempts to get and show an element with an <code>id<\/code> of <code>table<\/code>. I&#8217;m assuming that this was just a typo when posting this question and that, in reality, you really to have a table to show, but you need to correct that.<\/p>\n<p>Also, you should not be using HTML inline event attributes. This was the way we did event handling 20+ years ago before we had standards and unfortunately, is a technique that is so pervasive that it just won&#8217;t die the death it deserves. <strong>There are a variety of reasons not to use them.<\/strong> Instead, use modern standards and best-practices and do all of your event handling in separated JavaScript.<\/p>\n<p>You also need to add an additional line just before you attempt to select all the text in the password field to give that element the focus, otherwise the select code won&#8217;t work (see code below for this).<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\" data-console=\"true\" data-babel=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>\/\/ Get references to the elements you'll be working with\r\nvar input = document.getElementById(\"password\");\r\nvar div = document.getElementById(\"HIDDENDIV\");\r\nvar btn = document.getElementById(\"button\");\r\n\r\n\/\/ Set up event handlers in JavaScript\r\nbutton.addEventListener(\"click\", validate);\r\n\r\nfunction validate(){\r\n  if (input.value == 'PASSWORD') { \r\n     \/\/ No need to add a \"show\" class. Just remove the \"hidden\" class.\r\n     div.classList.remove('hidden');\r\n     \r\n     \/\/ Or, add it:\r\n     input.classList.add(\"hidden\");\r\n  } else {  \r\n     password.focus(); \/\/ &lt;-- If you don't do this first, your select code won't work\r\n     password.setSelectionRange(0, password.value.length);   \r\n     alert('Invalid Password!'); \r\n  }\r\n}\r\n\r\ninput.addEventListener(\"keydown\", function(event){\r\n if (event.keyCode === 13){\r\n   \/\/ No reason to simulate a button click. Just call the code that needs to be run.\r\n   validate();\r\n }\r\n});<\/code><\/pre>\n<pre class=\"snippet-code-css lang-css prettyprint-override\"><code>\/* You only need to apply this to elements that should be hidden and\r\n   then simply remove this class from hidden elements to show them. *\/\r\n.hidden { display: none; }<\/code><\/pre>\n<pre class=\"snippet-code-html lang-html prettyprint-override\"><code>&lt;input type=\"text\" id=\"password\"&gt;\r\n&lt;br&gt;\r\n&lt;input id=\"button\" type=\"button\" value=\"Login\"&gt;\r\n&lt;div id=\"HIDDENDIV\" class=\"hidden\"&gt;bla&lt;\/div&gt;<\/code><\/pre>\n<\/div>\n<\/div>\n<p><strong>NOTES:<\/strong><\/p>\n<ul>\n<li>Keep in mind that although this code can &#8220;work&#8221;, <strong>anyone can defeat<br \/>\nthis code quite easily<\/strong> simply by looking at your source code. To<br \/>\ntruly protect content from being consumed without the correct<br \/>\ncredentials being provided, you need to implement a server-side<br \/>\nsolution.<\/li>\n<li>Just like inline scripting should no longer be done, the same can be<br \/>\nsaid for using XHTML self-terminating tag syntax (i.e. <code>&lt;br \/&gt;<\/code>,<br \/>\n<code>&lt;input \/&gt;<\/code>). That is also an old syntax that just won&#8217;t go away.<br \/>\n<strong>Here&#8217;s why you don&#8217;t need and shouldn&#8217;t use this syntax.<\/strong><\/li>\n<\/ul>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">4<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Enter password to display content of div <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Because you hid the content via an id based CSS selector, adding a &#8220;show&#8221; CSS class to it later won&#8217;t override the id rule that you already set. (Read this on how different CSS selectors are more specific than others and thus, more difficult to override.) Here&#8217;s a quick example: #d1 { display:none; } &#8230; <a title=\"[Solved] Enter password to display content of div\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-enter-password-to-display-content-of-div\/\" aria-label=\"More on [Solved] Enter password to display content of div\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[346,333],"class_list":["post-19913","post","type-post","status-publish","format-standard","hentry","category-solved","tag-html","tag-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] Enter password to display content of div - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-enter-password-to-display-content-of-div\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Enter password to display content of div - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Because you hid the content via an id based CSS selector, adding a &#8220;show&#8221; CSS class to it later won&#8217;t override the id rule that you already set. (Read this on how different CSS selectors are more specific than others and thus, more difficult to override.) Here&#8217;s a quick example: #d1 { display:none; } ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-enter-password-to-display-content-of-div\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-07T21:31:17+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-enter-password-to-display-content-of-div\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-enter-password-to-display-content-of-div\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Enter password to display content of div\",\"datePublished\":\"2022-11-07T21:31:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-enter-password-to-display-content-of-div\\\/\"},\"wordCount\":353,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"html\",\"javascript\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-enter-password-to-display-content-of-div\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-enter-password-to-display-content-of-div\\\/\",\"name\":\"[Solved] Enter password to display content of div - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-11-07T21:31:17+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-enter-password-to-display-content-of-div\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-enter-password-to-display-content-of-div\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-enter-password-to-display-content-of-div\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Enter password to display content of div\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Enter password to display content of div - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-enter-password-to-display-content-of-div\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Enter password to display content of div - JassWeb","og_description":"[ad_1] Because you hid the content via an id based CSS selector, adding a &#8220;show&#8221; CSS class to it later won&#8217;t override the id rule that you already set. (Read this on how different CSS selectors are more specific than others and thus, more difficult to override.) Here&#8217;s a quick example: #d1 { display:none; } ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-enter-password-to-display-content-of-div\/","og_site_name":"JassWeb","article_published_time":"2022-11-07T21:31:17+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-enter-password-to-display-content-of-div\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-enter-password-to-display-content-of-div\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Enter password to display content of div","datePublished":"2022-11-07T21:31:17+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-enter-password-to-display-content-of-div\/"},"wordCount":353,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["html","javascript"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-enter-password-to-display-content-of-div\/","url":"https:\/\/jassweb.com\/solved\/solved-enter-password-to-display-content-of-div\/","name":"[Solved] Enter password to display content of div - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-07T21:31:17+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-enter-password-to-display-content-of-div\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-enter-password-to-display-content-of-div\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-enter-password-to-display-content-of-div\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Enter password to display content of div"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/19913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=19913"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/19913\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=19913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=19913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=19913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}