jquery - an unwanted page refresh when executing simple query code -
this question has answer here:
- prevent link reload page 5 answers
i want learn jquery can't figure out why page keeps refreshing right after on-click event done.
the goal code change background of divresult. works split seconds before page refreshed. tried on different browsers , different computers think it's code.
this jquery code:
$(document).ready(function(){ $('#button').on('click',function(){ var kleur = "#" +$('#text').val(); $('#divresult').css({'background-color': kleur}); }); });
and html file:
<!doctype html> <html> <head lang="en"> <meta charset="utf-8"> <script src="jquery_javascript_library_v1.11.3.js"></script> <script src="script.js"></script> <link href="style.css" rel="stylesheet"> <title></title> </head> <body> <form> <input type="text" id="text"> <button id="button">button</button> </form> <div id="divresult"> </div> </body> </html>
many in advance!
it's because default value type attribute of button elements "submit".
source : can make <button> not submit form?
solution :
<form> <input type="text" id="text"> <button type="button" id="button">button</button> </form>
Comments
Post a Comment