Counting hover times with jQuery -
i new javascript. want count how many times mouse hovers on word. tried code shown here - jquery count hover event - i'm not doing right.
for example:
<div> <p>the quick <a class="link1" style="color:#ff0000;">brown</a> fox jumps on <a class="link2" style="color:#ff0000;">lazy</a> <a class="link3" style="color:#ff0000;">dog</a>.</p> </div>
javascript:
jquery('.link1').mouseover(function(){ var $this = $(this); var count = parseint($this.data('count'), 10) + 1; $this.data('count', count); });
and repeat link2
, link3
"lazy" , "dog", doesn't work. can help, please?
problem is, anchor element doesnt have data-count
, cause error @ time of hover. add attribute work fine.
<div> <p>the quick <a class="link1" data-count="0" style="color:#ff0000;">brown</a> fox jumps on <a class="link2" data-count="0" style="color:#ff0000;">lazy</a> <a class="link3" data-count="0" style="color:#ff0000;">dog</a>.</p> </div>
jquery
$('.link1').mouseover(function () { var $this = $(this); var count = parseint($this.data('count'), 10) + 1; $this.data('count', count); alert($this.data('count')); });
Comments
Post a Comment