-1

Is there a way I can alert if the text inside has changed?

Start:

<span class="badge badge-cart">Shopping bag (0)</span>

on Click Changes to

<span class="badge badge-cart">Shopping bag (1)</span>

Therefore if the number inside the () has changed display alert

rob.m
  • 9,843
  • 19
  • 73
  • 162
  • why don't you put an alert inside the click event..? – Rajaprabhu Aravindasamy Jul 12 '14 at 12:43
  • because the alert should happen ONLY if the text has changed and not just on a click, what if it doesn't apply the submit? You would display an alert even tho you are not sure if it actually submitted. basically should work as a callback – rob.m Jul 12 '14 at 12:44
  • So in the click event test whether or not the quantity (or whatever other property) has changed; and then trigger an alert only if it has. – David Thomas Jul 12 '14 at 12:46
  • Why don't you give the span an id and bind something like this to your onclick event? $("#mySpanID").html("originaltext"); /*stored somewhere before click*/ vs. $("#mySpanID").html("newtext"); /*compared to stored value during onclick event*/ – The One and Only ChemistryBlob Jul 12 '14 at 12:47
  • @TheOneandOnlyChemistryBlob that var will start with 0 and when I first click it will store 1 and so on each time i click right? – rob.m Jul 12 '14 at 12:48
  • See also [this question](http://stackoverflow.com/q/1091661/735926). – bfontaine Jul 12 '14 at 12:49
  • Yes....I get slammed on this site for suggesting this, but I store values in forms as hidden inputs...then alter the value of the hidden input as necessary for your application – The One and Only ChemistryBlob Jul 12 '14 at 12:50
  • @bfontaine yup I have read many questions in regards before but with no luck I'm afraid – rob.m Jul 12 '14 at 12:56

1 Answers1

1
var $span = $('span.badge-cart'),
    $submit = $('input'),
    text = $span.text();

$submit.on('click', function () {
    if($span.text() !== text) {
        alert('text has changed');
        text = $span.text();
    }
});
jgillich
  • 71,459
  • 6
  • 57
  • 85
  • the click shouldn't be on the span but to the submit button, how it is not working. Mayeb because the number is updated with ajax? – rob.m Jul 12 '14 at 12:55
  • Updated my answer, you have to fix the selectors of course, but then it should work no matter how the number is updated. – jgillich Jul 12 '14 at 13:02