Joe
Joe

Reputation: 112

Detecting the text change jquery

Hi I am trying to detect if the value of the text of the div is "9" and then alert the user. But I cant get it working. Is there a way to detect the new number "9"? Thanks!

This is the live example: http://jsfiddle.net/7gyL2h2e/5/

HTML:

<div id="number">8</div>
<div id="clickme">Click me +1</div>

Jquery:

$("#clickme").click(function(){
      $('#number').html(function(i, val) { return +val+1;});
});

if($.trim($("#number").text())=="9"){
  alert("number 9");  

}

Upvotes: 0

Views: 46

Answers (3)

Rajashekhar Rangappa
Rajashekhar Rangappa

Reputation: 526

If you want to detect the number Then

$("#clickme").click(function(){
$('#number').html(function(i, val) { return +val+1;});
if($.trim($("#number").text())=="9"){
  alert("number 9");  
}

});

Upvotes: 1

Hemant
Hemant

Reputation: 2059

You need to call the function in click event to check

$("#clickme").click(function(){
  $('#number').html(function(i, val) { return +val+1;});
   checkme();

});

function checkme()
{
   if($.trim($("#number").text())=="9"){
  alert("number 9");  
 }
 }

Check the updated fiddle http://jsfiddle.net/7gyL2h2e/8/

Upvotes: 1

Adrian Bolonio
Adrian Bolonio

Reputation: 517

Just move the if condition inside the click event.

Check it here http://jsfiddle.net/7gyL2h2e/6/

HTML:

<div id="number">8</div>
<div id="clickme">Click me +1</div>

Jquery:

$("#clickme").click(function(){
  $('#number').html(function(i, val) { return +val+1;});
  if($.trim($("#number").text())=="9"){
    alert("number 9");  
  }
});

Upvotes: 1

Related Questions