Note that there are some explanatory texts on larger screens.

plurals
  1. POchange two values with one if statement
    primarykey
    data
    text
    <p>I'm just starting to learn javascript, so this is likely a pretty simple question. I've tried searching for an answer, but I think I just don't know enough to be like, 'oh, this looks different but answers my question too.'</p> <p>What I want to be able to do is to change the background color of a page based on the time of day, and also change an image based on the time of day. I have it working with an if...else if... statement for the background color placed in the head of the page, and a separate if...else if... statement affecting the image in the body.</p> <p>The head script that changes the bg color looks like:</p> <pre><code>var d=new Date(); var time=d.getHours(); if (time&gt;=0 &amp;&amp; time&lt;=5) { document.write ('&amp;lt;body style="background-color: 296688"&gt;') } else if </code></pre> <p>...and then the other times follow, each with a different color.</p> <p>The body script that changes the image looks like:</p> <pre><code>&lt;img src="" name="sunMoon" id="sunMoon" /&gt; &lt;script type="text/javascript"&gt; var d=new Date(); var time=d.getHours(); var elem = document.getElementById('sunMoon') if (time&gt;=0 &amp;&amp; time&lt;=5) { elem.src = 'Images/sunMoon1.png' } else if </code></pre> <p>...and then the other times follow, each with a different src.</p> <p>Is it possible to change the image AND the bg color using the same if...else if... statement in the head? I tried something like this in the head:</p> <pre><code>var d=new Date(); var time=d.getHours(); var elem=document.getElementById('sunMoon') if (time&gt;=0 &amp;&amp; time&lt;=5) { document.write ('&lt;body style="background-color: 2966B8"&gt;'); elem.src="images/sunMoon1.png" } else if... </code></pre> <p>but it didn't work.</p> <p>I think with the third (nonworking) example, either it's not possible to have a single if... do two things (change the bg color AND the image), or I'm just messing up the image code.</p>
    singulars
    1. This table or related slice is empty.
    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