Java script, button disappears after clicked.

sakthikumar
sakthikumar used Ask the Experts™
on
Hi

I am new to java script to HTML. Below is my code.
which displays a button. Problem is the button disappears after the execution onclick function.

Can you help me to fix this.

<html>
<head>
<title> My first java script program </title>
<style type="text/css">
    #play_button {
    position:absolute;
   
    top: 50px;
    left: 200px;
   
}
 </style>
</head>
<script type = "text/javascript">
function addnumbers(x,y){
var x = x+y;
return x;
}
function printnumbers(){
var x = addnumbers(12,23);
document.write(x);
}
//document.write(addnumbers(1,2));
</script>
<Form>
<div id = "play_button" >
<input  type = "button" value="click" onclick = "printnumbers(); return false;"></input>
</div>
</Form>
</html>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
What are you wanting to do
document.write(x);

Open in new window

this is not something you want to be doing after loading the document.
Here is some code that writes the result to a div on the page
Let us know what your objective is so we have a context.
 
<!doctype html>
<html>
<head>
<title> My first java script program </title>
<style type="text/css">
#play_button {
  position:absolute;
  top: 50px;
  left: 200px;
}
</style>
</head>
<script type = "text/javascript">
function addnumbers(x,y)
{
  var x = x+y;
  return x;
}
function printnumbers()
{
  var x = addnumbers(12,23);
  document.getElementById('output').innerHTML = x;
}
//document.write(addnumbers(1,2));
</script>
<Form>
  <div id = "play_button" >
    <input  type = "button" value="click" onclick = "printnumbers(); return false;"></input>
  </div>
  <div id="output"></div>
</Form>
</html>

Open in new window

Author

Commented:
I tried to call a function from a function that calculates a value.

and wanted to display the value on clicking of a button.

When I did that button gets disappeared.

Your solution is correct. Thank you.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial