Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is how you would bind a click handler to your <code>div</code> and add the class to your header:</p> <pre><code>var yourDiv = document.getElementById('your-div-id'); var header = document.getElementById('header'); yourDiv.onclick = function(){ alert("yourDiv was clicked"); header.className = "newCssClass"; }; </code></pre> <p>The above assumes markup like so:</p> <pre><code>&lt;div id="your-div-id"&gt;Click&lt;/div&gt; &lt;div id="header"&gt;&lt;/div&gt; </code></pre> <p>Here's <a href="http://jsfiddle.net/mp8Lq/" rel="nofollow">an example</a>.</p> <p><em>Update:</em> The reason that the <code>expanded</code> variable isn't working as you'd expect is because you're creating a duplicate local variable called <code>expanded</code> in your <code>btnClick()</code> method. As a result, the global <code>expanded</code> variable you declare outside the function is never updated.</p> <p>This is being caused by how you're using the <code>var</code> keyword:</p> <ol> <li>When used outside a function, <code>var</code> creates a global variable that's accessible anywhere within the current document.</li> <li>When used inside a function <code>var</code> creates a local variable that is accessible only within that function.</li> </ol> <p>Here's your function cleaned up to work as you'd expect:</p> <pre><code>// Define global variable (using var outside function) var expanded = true; function btnClick(){ alert('The variable expanded is '+expanded); // Condition for true if(expanded) { // do something // Condition for false } else { // do something else } // Flips boolean value of the global variable (notice lack of var keyword) expanded = !expanded; } </code></pre> <p>Here's <a href="http://jsfiddle.net/mp8Lq/2/" rel="nofollow">an example</a> showing the correct way to update the <code>expanded</code> variable.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload