Forum Moderators: open
function blah(){
var buffer=document.createElement('div');
buffer.innerHTML=this.txt2.value;
document.getElementsByTagName('body')[0].appendChild(buffer)
var letters=buffer.childNodes;
var newl=new Array();
for(var i=0;i<letters.length;i++)
{
if(letters[i].innerHTML)
{
var temparr=letters[i].innerHTML.split("")
for(var x=0;x<temparr.length;x++)
{
if(letters[i].tagName!="a"&&letters[i].tagName!="A")
{
newl[newl.length]=document.createElement(letters[i].tagName)
newl[newl.length-1]=applyStyle(letters[i].style,newl[newl.length-1])
newl[newl.length-1].appendChild(document.createTextNode(temparr[x]));
}
else
{
newl[newl.length]=letters[i]
}
}
}
else
{
newl[newl.length]=letters[i]
}
}
buffer.innerHTML=""
for(var i=0;i<newl.length;i++)
{
buffer.appendChild(newl[i]);
}
var rstring=buffer.innerHTML;
document.getElementsByTagName('body')[0].removeChild(buffer)
return rstring;
}
eg:
document.getElementsByTagName('body')[0].appendChild(buffer); I'm not convinced by the manipulation of innerHTML, although this may be OK? I would tend to perhaps createTextNode(this.txt2.value) instead... if this.txt2.value is intended to be text?! Hhhmm, maybe not?
What does applyStyle() do? May be the problem is in there?
I guess I should have said its purpose's earlier. It basicly splits up an HTML tag and makes it into many smaller tags. so:
<span style="font-size:10px">Hello</span>
Becomes
<span style="font-size:10px">H</span><span style="font-size:10px">e</span><span style="font-size:10px">l</span><span style="font-size:10px">l</span><span style="font-size:10px">o</span>
Penders, applyStyle defiantly works. I tested it separately and it works, the fault is in there somewhere. Also I need to use innerHTML for this purpose becuase in firefox (and maybe other browsers) it would convert characters like < and > to > and < and but I need tags to remain as tags so using createTextNode is out of the question.
Dabrowski, Thanks for the advise.
buffer.innerHTML=this.txt2.value;
What does this.txt2.value actually contain?
newl[newl.length]=document.createElement(letters[i].tagName)
newl[newl.length-1]=applyStyle(letters[i].style,newl[newl.length-1])
You're creating an element, for example, newl[0] = a new div. You're then overwriting it with the value from applyStyle, you've got newl[0] = applyStyle..... so it now no longer is your new div. I suspect this may be where the problem is. If you can tell me the syntax and object of applyStyle I'm sure I can cut this code down somewhat.
newl[newl.length]=document.createElement(letters[i].tagName)
newl[newl.length-1]=applyStyle(letters[i].style,newl[newl.length-1])You're creating an element, for example, newl[0] = a new div. You're then overwriting it with the value from applyStyle, you've got newl[0] = applyStyle..... so it now no longer is your new div. I suspect this may be where the problem is. If you can tell me the syntax and object of applyStyle I'm sure I can cut this code down somewhat.
@Dabrowski
Actually, it looks as if the value from applyStyle is assigned to the previous array element (Note: -1), to give something such as newl[-1] = applyStyle() ...?
newl[newl.length]=applyStyle(letters[i].style,document.createElement(letters[i].tagName))
@penders The length property of an array in javascript is always one greater than its biggest numerical key. so newl[newl.length]="blah" is the same as newl.push("blah") so newl[newl.length-1] is the last added value or the new plain div.
so change
if(letters[i].innerHTML)
to
var txt = document.all? letters[i].innerText : letters[i].innerHTML;
if(txt && txt!= '')
this may be the problem, I'm not sure, but it will be more forgiving for old IE versions anyway.
also something else I noticed, if you are not sure about the content in this.txt2.value then letters array contain different nodeType's than your not expecting. So your first line after
for(var i=0;i<letters.length;i++)
should check for nodeType. Also, letters does not need to contain all the nodes..
such as...
for(var i=0;i<buffer.childNodes.length;i++){
if(buffer.childNodes[i].nodeType!=1){continue} //nodeType 3 would be text node
var temparr = document.all? buffer.childNodes[i].innerText.split("") : buffer.childNodes[i].innerHTML.split("");
if(temparr.length>0){
if(letters[i].tagName.toLowerCase()!="a"){
}else{}
}
}
does any of that mess help?
function splitString( obj) { Where obj would be a reference to an element to take innerHTML from?
Do you only want bare text, i.e.
"foo bar" would split all characters into your SPANs....
"foo <b>bar</b>" would only split "foo " as "bar" is contained within another tag.
Don't mean to put you off, what you want is certainly possible, we just need details.