vendredi 7 octobre 2016

Javascript innerHTML find and replace approach is wrong? (No JQuery)

I have a page where I implemented a find and replace utility.

I did use the approach described into this post:

javascript replace text in the html body

But because innerHTML returns to the function that performs the find/replace a String representing the HTML content of the target element, such approach has a huge issue in some situations.

For example, in my case I have a table with a lot of data records (rows). Into the last field of each record there is a checkbox. After performing the "find/replace" function with the above specified approach, all checkboxes are resetted to their default value, loosing the information stored in their "checked" property.

In other words after a "find-replace" execution, all checked checkboxes are set as unchecked.
My conclusion has been that the innerHTML approach for find and replace is wrong if the page has objects that need to preserve their properties.

My code:

    findRepTgt.innerHTML = findRepTgt.innerHTML.replace(searchSource, replaceWidth); 

where findRepTgt is the tbody element.

My questions is:
If my conclusion is right, how do I solve such issue?




Aucun commentaire:

Enregistrer un commentaire