javascript - How JQuery objects assigned to a variable detect changes? -


(first of sorry english) can understand assigning jquery objects found jquery selectors, variable has better performance using jquery selectors every time. part cannot understand how jquery detects changes not manipulated jquery? here example:

<div id="divstate">first state</div>  $(document).ready(function () {      var testelement = $("#divstate");      alert(testelement.text());//alerts "first state".      document.getelementbyid("divstate").innerhtml = "second state";//div has changed outside of jquery object.      alert(testelement.text());//alerts "second state"??? how variable detects changes. }); 

as understand it, jquery object contains dom node, live object. changing properties of dom node reflected in jquery object.

if .clone() jquery object, you'd behavior you're expecting:

$(document).ready(function() {    var testelement = $("#divstate").clone(); // copy object , store    alert(testelement.text()); //alerts "first state".    document.getelementbyid("divstate").innerhtml = "second state"; //div has changed outside of jquery object.    alert(testelement.text()); //alerts "first state" again  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <div id="divstate">first state</div>


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -