开发者

Update when value changed jquery/javascript?

开发者 https://www.devze.com 2023-03-01 09:42 出处:网络
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 a

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

精彩评论

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