jQuery $.each() not saving changes
by fiddleyetu
HTML
<span id="source"></span><br /><br />
<button onclick="doReplace()"
title="Replace all change with done">
Replace
</button><br /><br />
<span id="result"></span>
<div id="debug"></div>
CSS
span { color: Blue; }
JavaScript
//basic custom object with two properties
var obj = {
testA: "please change this",
testB: "and change this too"
};
//display original text
$(document).ready(function() {
$("#source").text("Source: " + obj.testA + " + " + obj.testB);
});
//search/replace properties and display result
function doReplace() {
$.each(obj, function (key, value) {
$('#debug').append( '<br><br><b>key = ' + key + '; value = "' + value + '"</b><br>')
.append( '<br>Equality TEST: ' + (this == value) )
.append( '<br>Output TEST - using value: ' + value.replace('change','done') )
.append( '<br>Output TEST - using "this": ' + this.replace('change','done') );
obj[ key ] = this.replace('change','done');
});
$("#result").text("Result: " + obj.testA + " + " + obj.testB);
}