jquery - an unwanted page refresh when executing simple query code -


this question has answer here:

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">&nbsp;</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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -