开发者

Update when value changed jquery/javascript?

So when I initally use .append to append a string variable in javascript "myvar" to a div tag. If later on the "myvar" is set to some other string.. I want the div tag with th appended myvar (or anywh开发者_开发百科ere that uses the myvar variable) to be updated to reflect the new value as well. How to accomplish this in jquery/javascript?


Every time you change the value of myvar you'll need to update the HTML as well. You could wrap that in something nice like:

function setMyVar(val) {
    myVar = val;
    $('#myVar').html(val);
}

You would need to adjust your initial append a little to accomodate this:

$('div').append('<div id="myVar">' + myVar + '</div>');


There is now real time automatic data binding of variable -> text of dom element in HTML and jQuery by the nature of JS - there is no event like "variable changed" or so.

You can emulate live data binding for example like this: http://www.grantshepert.com/post.cfm/jquery-and-data-binding-to-form-fields But this will also require function invocation when data changes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜