Damon
Damon

Reputation: 10799

event listener in jquery for variable change

I want to track a variable as I navigate around my site. How can I use jQuery to output this value without having to worry about tracking down wherever it might be affected and having it update.

var num = 1;

$(#trackvariable).text(num)  

I want the above to execute constantly as it were.. I don't necessarily want to do it on a timer. Is there any way to do that?

Upvotes: 1

Views: 1563

Answers (3)

Mrchief
Mrchief

Reputation: 76198

If you want to listen to changes on a textnode, you can do something like this:

function InitListener () {
    var container = document.getElementById ("textContainer");
    var textNode = container.firstChild;
    if (textNode.addEventListener) {
        textNode.addEventListener ('DOMCharacterDataModified', OnCharacterModified, false);
    }
}

Upvotes: 1

nhutto
nhutto

Reputation: 163

Off the top of my head I would say that the functions changing the variable should just call some sort of update function at the end of them? just an idea. I think even if you set up some kind of custom event it would still have to manually triggered in each function.

Upvotes: 0

Maz
Maz

Reputation: 3375

This will only work in Firefox (and probably Chrome in Safari also):

https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects#Defining_Getters_and_Setters

Upvotes: 0

Related Questions