Note that there are some explanatory texts on larger screens.

plurals
  1. POVariables for images
    primarykey
    data
    text
    <p>im trying to make a switch wich will change two images. I once solved ths, but then i lost some important files, the one containing the final script being one. </p> <p>The idea is that when the button is clicked, it will change image 1 for image 2 and will change its own image from on to off. Then, when clicked again it will change image 2 for image 1 and its own image from off to on. I been trying something like this, buts not working, not sure why. I think i got the wrong declaration for the if which determines if the switch is on or off, but again not sure. </p> <p>Before you read the code and realize its poorly done, consider i dont know a thing about javascript, i only have a vague idea of how it works.</p> <pre><code> &lt;script type="text/javascript"&gt; var vswitch = false; if (document.getElementById("switchh").src = "http://www.sampleweb.com/on.gif") { vswitch = true } else { vswitch = false } function change(){ if (vswitch == true){ function changelamp() { document.getElementById("lamp").src = "http://www.sampleweb.com/image2.png"; } function changeSwitch() { document.getElementById("switchh").src = "http://www.sampleweb.com/off.gif"; } } else { function changelamp() { document.getElementById("lamp").src = "http://www.sampleweb.com/image1.gif"; } function changeSwitch() { document.getElementById("switchh").src = "http://www.sampleweb.com/on.gif"; } } } &lt;div id="main_img"&gt; &lt;img id="lamp" src = "http://www.sampleweb.com/image1.gif"&gt; &lt;/div&gt; &lt;div id="container"&gt; &lt;img id="switchh" src = "http://www.sampleweb.com/on.gif" onclick='change();'&gt; &lt;/div&gt; &lt;/script&gt; </code></pre> <p>Thank you</p> <p>/////////////////////EDIT/////////////////////////// Thanks a lot. Having those two functions there was a result of the previous code, i dont understand how i didnt realize it until you pointed out, heh. (Sleepyness maybe?) @renuka, that code worked perfectly. I only changed the calling div, from the div "toggle" you created to the div "container" since the button has to switch the images itself, but other than that was sweet. Thanks. Thanks for the help!</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.
 

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