I'll illustrate with an example: I need to convert the following html with javascript
<a>Text 1</a>
<a>Text 2</a>
<a>Text 3</a>
...
to code
<a><input/>Text 1</a>
<a><input/>Text 2</a>
<a><input/>Text 3</a>
...
I don't have a clue how to achieve that with createElement, appendChild or insertBefore/After.
.insertBefore, and .firstChild
You could insert your new input element before the first child of each anchor:
Demo: http://jsbin.com/ibugul/edit#javascript,html
Regular Expression
Or you could use the
replace
method:Modify .innerHTML
jQuery
If you're already using jQuery on your site, you could use that to make this even shorter:
Note, it is not worth including the library just for this.
It's not that hard :)