javascript - Displaying Dynamic Circle in html page using SVG -


the below code simple form accepts number user , dispalys circle of radius entered in form. working fine except circle vanishes click on submit button. how fix this? want circle there long after clicked on submit button.

<title>dynamic vector circle</title>  <script type="text/javascript">      var svgns = "http://www.w3.org/2000/svg";        function createcircle()     {         var mycircle = document.createelementns(svgns,"circle"); //to create                                          circle, rectangle use rectangle         var radius= document.getelementbyid("rad").value;         mycircle.setattributens(null,"id","mycircle");         mycircle.setattributens(null,"cx",100);         mycircle.setattributens(null,"cy",100);         mycircle.setattributens(null,"r",radius);         mycircle.setattributens(null,"fill","black");         mycircle.setattributens(null,"stroke","none");          document.getelementbyid("mysvg").appendchild(mycircle).innerhtml;     }                   </script> 

<form>        <input type="text" id="rad" placeholder="enter radius">     <button onclick="createcircle();">submit</button>      <svg id="mysvg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> </svg>  </form> 

the problem default action form on button click submit. avoid submitting, can return false function bound button. if change end of createcircle to:

  ...   return false; } 

it work desired. here whole function:

function createcircle() {     var mycircle = document.createelementns(svgns,"circle"); //to create                                          circle, rectangle use rectangle     var radius= document.getelementbyid("rad").value;     mycircle.setattributens(null,"id","mycircle");     mycircle.setattributens(null,"cx",100);     mycircle.setattributens(null,"cy",100);     mycircle.setattributens(null,"r",radius);     mycircle.setattributens(null,"fill","black");     mycircle.setattributens(null,"stroke","none");      document.getelementbyid("mysvg").appendchild(mycircle).innerhtml;      return false; } 

jsfiddle: http://jsfiddle.net/252yldqx/2/

or can change button input type="button" not have default behavior submit form:

<input type="button" id="create" value="create"/>

jsfiddle: http://jsfiddle.net/252yldqx/3/

or can change button have type="button" override default type="submit" so:

<button type="button" onclick="createcircle();">submit</button>

or can not enclose inputs within form tag , whatever like!


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -