I need to run a search and replace on HTML similar to the following... I need to have \"Find Next\" \"Replace\" and \"Replace All\" options.... the trick is that I need to r
I would build a data object while traversing matched elements. Then send the object so you do not have multiple ajax request from a loop. jsfiddle:
This is yet another test
This is test data
Script:
var searchTerm = 'This is',
replaceWith = 'This is new',
updateObj = {};
$("#sheet div.item:contains('" + searchTerm + "')").each(function(){
// use id to build an update object
updateObj[this.id.replace('field-', '')] = {
oldText: searchTerm,
newText: replaceWith
}; // not sure what you are trying to save here
// manipulate html
this.innerHTML = this.innerHTML.replace(searchTerm, replaceWith);
});
// after, send ajax data `updateObj`