javascript - Why is this method called multiple times? -


when refresh page, must click button twice in order prompt. after that, each time click same button , call classichover() method (without refreshing), 1 plus prompt(two prompts, 3 prompts, four...).

this code:

function classichover(){     $('#button1').click(function(){         gridsize = prompt("set grid size")         $('.container').empty();          for(i = 0; < gridsize * gridsize; i++) {             $('.container').append("<div>a</div>");         };     }); }; 

button:

<input id="button1" type="button" value="set new grid" onclick="classichover()" /> 

what doing wrong? js/jquery newbie btw

your click calls classichover. classichover adds function button's listener. each time click it, add listener. first click adds 1. second click runs 1 , adds another. third click runs 2 , adds another. repeat each click.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -