jeudi 26 mars 2015

Replacing part of string with a HTML Element

I'm using plain Javascript to replace part of a string with an HTML Element.


I'm using the following script to do what I need, consider var data to be an array of objects where data[objectAtIndex].text has the following example string {0} is the right answer, but it can also contain Something {0} fishy. The {0} part of the string can change place at any time.



var select = document.createElement("select"),
textDiv = document.createElement("div");
textDiv.innerHTML = data[i].text.replace(pattern, select);


The regular expression I use looks like var pattern = /\{0\}/i;


Now, this halfway works, only it converts the select element to string, which yields [object HTMLSelectElement] instead of the actual element itself.


I want the actual element, and not the string representation of this, and I can't really find any answers which doesn't utilize jQuery.


Aucun commentaire:

Enregistrer un commentaire