javascript - Search and replace in an $.each() -
i have code below loops on section of dom, searches 3 different strings, , replaces each else. need make arbitrary number of items can searched , replaced, instead of 3. possible jquery?
function getdomarray(domclone){ var postrows = []; $(domclone).each(function () { var find1 = "__hidden__"; var find2 = "__bigtext__"; var find3 = "col-sm-12"; var re1 = new regexp(find1, "g"); var re2 = new regexp(find2, "g"); var re3 = new regexp(find3, "g"); $(this).html(function (index, html) { return html.replace(re1, '').replace(re2, 'bigtext').replace(re3, "col-sm-4"); }); postrows.push($($(this).html()).encodehtml()); }); return postrows; } update 1: code in @jonathancard's answer throws error:
cannot read property 'replace' of undefined
please see jsfiddle: http://jsfiddle.net/jsfiddleplayer/vhg7csb7/12/
update 2: code in answer works!
try this:
function getdomarray(domclone) { var postrows = []; list_to_replace = {"__hidden__": '', "__bigtext__": "bigtext", "col-sm-12": "col-sm-14"}; $(domclone).each(function() { $.each(object.keys(list_to_replace), function(index, item) { var re = new regexp(item, "g"); $(domclone).html(function (index, html) { return html.replace(re, list_to_replace[item]); }); }); postrows.push($(domclone).html()); }); return postrows; } edit: sorry confusion. should work, point out returns text clone, doesn't perform replacement since working on clone.
Comments
Post a Comment