Javascript: Replace all P tags with Div (without any library)

javascript

Solution

`getElementsByTagName` returns a live node list so every time you replace a node the list changes, so you only want to get the first node in the list and replace it until the list is empty, see http://jsfiddle.net/mowglisanu/eZNqn/4/

Problem

Below is a code snippet ``` var pTags=document.getElementsByTagName('p'); for(i=0;i<pTags.length;i++) { var p=pTags[i], div=document.createElement('div'); div.innerHTML='P tag replaced with a div tag'; p.parentNode.replaceChild(div, p); } ``` It should replace all `P` tags with `Div` but it's not replacing all but some of them. The `red` ones (fiddle) are not replacing. I don't need this but I want to know what is I'm doing wrong here ? So, my question is why not it's working in this way. Hope someone can tell me the fact. Thanks for your effort!

Original source

Related problems