i have my code(html/javascript) here which allows user to input the size and color of the square.The problem is when i click the go button the textfields disappear including the button and the one that displays is the output.Is there any ways that i could retain the textfields and the button then output the square below it?
<html>
<head>
<script type="text/javascript">
size=0;
function display(form){
size=form.inputbox.value;
color=form.inputbox2.value;
for(i=0;i<size;i++){
for(j=0;j<size;j++){
if(i==0||i==(size-1)){
document.write("<font color="+color+">* ");
}
else if (j == 0||j == (size-1)) {
document.write("* ");
}
else {
document.write(" ");
}
}
document.write("<br>");
}
}
</script>
</head>
<body>
<form name="form1" action="" method="get">
Input size of the square:
<input type="text" name="inputbox" value=""><br>
Input color of the square:
<input type="text" name="inputbox2" value="">
<input type="button" name="button" value="Go" onClick="display(this.form)">
</form>
</body>
</html>
Calling
document.writeon a page that has completed rendering will destroy the exiting DOM document on the page, replacing it with only the newly written HTML elements.If you wish to add to existing DOM elements, select an element and write to its
innerHTMLproperty instead.And add an empty node to your HTML something like:
Here it is in action on jsFiddle.net
To get the square to be all the correct color, it is easiest to change the CSS color property of
<div id='writeTo'>