Jump to content

Document.write


Fire Dragon

Recommended Posts

I have little problem about document.write code.Okay,I made little exercise code,where is two buttons.Another them increases variable,and other decreases it.So,I made it so,that when variable is "52",document.write creates image to screen.However,it appeared above those two buttons,and I can't press them!So question is,how I can control,where document.write creates picture?And one extra question,if code creates image when number is above 52,how I can set it disappear,when variable is under 52?Thanks.And last,here is my code.It contains some Finnish words,but I think that you can still understand my code :)

<html><head><script type="text/javascript">var mutu=50function cnt(){mutu=mutu+1if(mutu == 52){document.write('<img src="lotad.gif">')}}var mutu=50function minus(){mutu=mutu-1if(mutu == 52){document.write('<img src="lotad.gif">')}}</script><script type="text/javascript">function tarkista(){document.getElementById('txt').value=mutu}</script></head><body><form><input type="button" onclick="cnt()" value="Lisää"></form><form><input type="button" onclick="minus()" value="Vähennä"></form><form><input type="button" onclick="tarkista()" value="Näytä tämän hetkinen luku."></form><form><input type="text" id="txt"></form></body></html>

Link to comment
Share on other sites

The document.write method will overwrite the current document and enter the text you specify in its place when invoked as a function. You should only use it to output HTML while the page is being loaded. Since you are invoking it as a function which is called by a button, the page's HTML will have finished loading by the time it is used, and therefor the document will be overwritten with your document.write HTML.

Link to comment
Share on other sites

Okay...But how I then can place image appear,when variable is 52?Or is it completely impossible?Heeelp! :)

put the image in the page as normal,give it an id and set it's display to none<img src="lotad.gif" id="pic" style="display:none">when the variable is above 52 use javascript to make the image appeardocument.getElementById('pic').style.display="block"; :)
Link to comment
Share on other sites

Thanks,it works nicely!But do anyone know,how make pictures disappear,when variable is lower than 52?This thing isn't my biggest problems now,but it would be nice,if I can learn it :) Thanks.

try this: :) if(mutu < 52){document.getElementById('pic').style.display="none";}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...