i have this simple code which is replacing the first span with a div ,
here is LIVE examples :
- the original working one : http://jsfiddle.net/SNmLJ/5/
- the one with the changes : http://jsfiddle.net/SNmLJ/6/
–
var pn = "parentNode" , rc = "replaceChild";
var elem = document.getElementsByTagName('span')[0];
var newE = document.createElement('div');
newE.innerHTML = 'test';
elem[pn][rc](newE , elem);
the code is perfectly working , so i tried to short the code a bit so i did this :
var pn = "parentNode" , rc = "replaceChild";
var newE = document.createElement('div');
newE.innerHTML = 'test';
document.getElementsByTagName('span')[0][pn][rc](newE , elem);
as you see i took the “elem” var out and replace the value in the bottom of the code ,
i don’t know why it’s not working but i guess the problem is causes of the “document” object in the first of the line.
- single question : why it’s not working properly , what have been gone wrong here?
The problem is that you forgot that the
elemvariable was used also in the last parameter passed to thereplaceChildfunction.Here is elem at the end, but you have erased the definition:
document.getElementsByTagName('span')[0][pn][rc](newE ,elem);