0

I have a span with a value which is asynchronously updated. so, I want jquery listen to a value and if it equals to, let's say "test" I want to hide another html element. I cannot figure out what event should I use, since .change is only valid for textboxes and selects.

user194076
  • 8,787
  • 23
  • 94
  • 154
  • 2
    possible duplicate of [jQuery: Listen to changes within a DIV and act accordingly](http://stackoverflow.com/questions/2712124/jquery-listen-to-changes-within-a-div-and-act-accordingly) – j08691 May 23 '12 at 17:16
  • 2
    why don't you put the logic of checking the value of span and hiding the html element in the code which sets the span value? – Pencho Ilchev May 23 '12 at 17:16
  • what causes the span to update? – Adam Sweeney May 23 '12 at 17:17

2 Answers2

0

Can you access the callback function of the method that's asynchronously updating the value? If so, you can drop a conditional/listener in there that analyzes the response. Something like this:

$.ajax({
   success: function(responseData) {
      if(responseData.specificKeyValue == 'test') {
          $('element to hide').hide();
      }
   }
});
Jon Friskics
  • 292
  • 2
  • 9
0

I would do something like below. Which basically triggers a custom event when you update the span.

Note: Below is a demo code, try to get the idea and implement accordingly.

$(function() {
    var $result = $('#result');
    setInterval(function() {
       $result.append('Span Updated').trigger('spanChange');
    }, 1000);

    $result.on('spanChange', function () {
        //your custom span change listener
        console.log('Span got updated');
    });
});

DEMO

Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
  • Now this code triggers only every second because of settimeout. How can I make it to trigger on text changed inside a div? – user194076 May 23 '12 at 17:25
  • How do you change the text inside div? Just add a trigger like `.trigger('spanChange')` whenever you change the text inside div. – Selvakumar Arumugam May 23 '12 at 17:26
  • it is done by telerik controls. this was my original questions, but still cannot find solution, so i thought I can try hiding it with jquery: http://www.telerik.com/community/forums/aspnet-ajax/upload/hide-radprogressarea-after-response-end.aspx – user194076 May 23 '12 at 17:32
  • @user194076 Two things: **1.** `span` doesn't have an `onchange` event. **2.** No event will be triggered when you do it using a script (ex: `$('input').val('Test')` will not trigger `onchange` event). – Selvakumar Arumugam May 23 '12 at 17:38