Jump to content

JS Generator


EmperorZenos

Recommended Posts

I'm trying to make a small generator for a friend and I'm getting a problem with this code:

    <form>    <input type="text" id="name"><br />    <input type="text" id="po"><br />    <input type="submit" onclick="Gen()"></form>    <script type="text/javascript">    function Gen() {    var A=document.getElementById("name").value;    var B=document.getElementById("po").value;    var HPba=100;    var la=po/10;    var hea=HPba+la;    var na=po/12;    var DEFba=5;    var DEF=DEFba+na;    if (po>10) {    document.write("Health: ");    document.write(hea "<br />");    document.write("Defense: ");    document.write(DEF "<br />");    }    else    {    document.write("Health: ");    document.write(HPba "<br />");    document.write("Defense: ");    document.write(DEFba "<br />");    }    }    </script> 

If some of you haven't noticed I would get, Filename missing, each time I click on the submit button.I've tried everything, even asking a skilled friend of mine, none of it worked, whats wrong with it?

Link to comment
Share on other sites

You don't need a <form> tag for this because you're not sending data to the server.Instead of a submit button, use a normal button:<input type="button" onclick="Gen()" />Another problem: You need a concatenating operator to join variables and strings:hea+"<br />"Finally, using document.write() is going to blank out your whole page before writing in it. Use DOM methods or innerHTML to write.

Link to comment
Share on other sites

You don't need a <form> tag for this because you're not sending data to the server.Instead of a submit button, use a normal button:<input type="button" onclick="Gen()" />Another problem: You need a concatenating operator to join variables and strings:hea+"<br />"Finally, using document.write() is going to blank out your whole page before writing in it. Use DOM methods or innerHTML to write.
Could you give me an example?I'm not very good DOM, and I don't get the w3school tutorial of it.
Link to comment
Share on other sites

  <form>	<input type="text" id="name"><br />	<input type="text" id="po"><br />	<input type="submit" onclick="Gen()"></form><div id="container"> </div>	<script type="text/javascript">	function Gen() {	var A=document.getElementById("name").value;	var B=document.getElementById("po").value;var C=document.getElementById('container');	var HPba=100;	var la=po/10;	var hea=HPba+la;	var na=po/12;	var DEFba=5;	var DEF=DEFba+na;var str=''; str += (po>10) ? "Health: "+hea+ "<br />Defense: "+DEF "<br />" : "Health: "+HPba+ "<br />"Defense: "+DEFba+ "<br />");C.innerHTML = str;	}	</script>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...