javascript - Why does badge count go from 1 to 12? -
when receiving message in chat program, count should increment 1. jumps 1 12. why that?
$("#btn").click(function(){ var count = number($('.badge').text()); $('.badge').text(count + 1); });
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-kj3o2dktikvyik3uenzmm7kckrr/re9/qpg6aazgjwfdmvna/gpgff93hxpg5kkn" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/u6ypibehpof/4+1nzfpr53nxss+glckfwbdfntxtclqqenisfwazpkamnfnmj4" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0abixch4zdo7tp9hkz4tshbi047nrkglo3sejag45jxxngifyzk4si90rdiqnm1" crossorigin="anonymous"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span class="badge">0</span> <button id="btn"></button>
i able reproduce problem putting two elements class badge
in same page. causes $('.badge').text()
return 11
, incremented 12
stored in each badge. if intended have 1 badge, rid of other one. or perhaps meant give them each different names?
otherwise, increment each badge separately:
$(".badge").each(function(idx){ var count = number($(this).text()); $(this).text(count + 1); });
Comments
Post a Comment