0

I think this is a super basic question, but I'm a little confused.

If I want to take a snapshot of the text-value of a jQuery object that changes its values dynamically, how would I do this?

For example, var text = $('#div').text() will make text equal to whatever div's text value is at the moment it is referenced, but I want it to always equal the div's text value at the instant the expression was set.

user2066880
  • 4,825
  • 9
  • 38
  • 64
  • look at event handlers like keyDown, keyPress in jQuery – Venkata Krishna Aug 09 '13 at 18:26
  • I don't think this is a basic question. You could be explicit and do something like what Bucky24 or insertusernamehere propose below, but depending on how you plan to use the value you'll probably end up reimplementing something that looks like the dynamic bindings offered by frameworks like knockout.js or angularjs; check them out – Francisco Meza Aug 09 '13 at 18:58

3 Answers3

0

You can store the selector in a variable:

var container = $('#div');

Then you can access its text anytime you want. It will always be the most recent content:

if ('value' == container.text()) {}
insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
0

You might be able to do this using the change event.

IE:

var text = $("#div").text();
$("#div").on("change",function() {
    text = $("#div").text();
});

The change event will fire every time focus moves from the specified element, so it won't happen on every keystroke.

According to How can I get jquery .val() AFTER keypress event?, you can use the keyup event instead of change, to get the value immediately after it changes.

Community
  • 1
  • 1
0

You could add them as a query string or keep them in a cookie.

varun
  • 1