Accepted answer

It only persists for a short time because you have placed your button within a form here:

<form name="contact" action="" method="post">
    <button id="control" onclick="js_fun()">Click me</button> 

When clicking the button the form is submitted. Since action="" it submits the form to the same page, and you once again have to call js_fun() to render the graph. The graph is only shown the short time between clicking the button and the new page being loaded.

You could make your button code like this:

    <button id="control" onclick="js_fun(); return false">Click me</button> 

Or just remove the form tag if you're not actually planing on submitting a form at any time.


If you does not give any type for the button tag element, it will point as submit. when you click the button, the form is submitted first after that only JavaScript calls.You did not mention any action for the form tag, so form is submitted to the same page.So button JavaScript click event does not call because the page is refreshed each button click.

You have 3 ways to resolved this type issues

1.If you does not post any form values then remove the form tag, just put only the button tag.

2.include the return false in the button on-click event.

     <button id="control" onclick="js_fun(); return false">Click me</button> 

3.Include button type="button" in the button tag element.

      <button id="control" type="button" onclick="js_fun();">Click me</button> 

Related Query

More Query from same tag