Forum Moderators: open
function swapText(id, text1, text2){
var elem;
elem = document.getElementById(id);
alert(elem.innerHTML);
elem.innerHTML = (elem.innerHTML == text1)? text2 : text1;
}
It's working fine except when the string contains ">>". I tried calling it the following ways and neither of them work:
<a onclick="swapText('optionslink','Options >>','Hide Options >>');" id="optionslink">Options >></a>
<a onclick="swapText('optionslink','Options >>','Hide Options >>');" id="optionslink">Options >></a>
Any ideas?
In this instance, the "
>" symbol when octal-encoded gives you [b]\u003E[/b] - so you should try the following: <a onclick="swapText('optionslink','Options [b]\u003E\u003E[/b]','Hide Options [b]\u003E\u003E[/b]');" id="optionslink">Options [b]>>[/b]</a>
<a onclick="swapText('optionslink','Options \u003E\u003E','Hide Options \u003E\u003E');" id="optionslink">Options \u003E\u003E</a>
function htmlEsc( iS ) {
var aS = iS;
var re = /"/g;
aS = aS.replace(re, """);
re = /'/g;
aS = aS.replace(re, "'");
re = /</g;
aS = aS.replace(re, "<");
re = />/g;
aS = aS.replace(re, ">");
return aS;
}
function swapText(id, text1, text2){
var elem;
var t1 = htmlEsc( text1 );
var t2 = htmlEsc( text2 );
elem = document.getElementById(id);
alert(elem.innerHTML);
elem.innerHTML = (elem.innerHTML == t1)? t2 : t1;
}
[edited by: daveVk at 2:03 am (utc) on Oct. 14, 2007]
<html>
<script>
function swapText(id, text1, text2){
var elem;
elem = document.getElementById(id);
elem.innerHTML = ((elem.innerText¦¦elem.textContent) == text1)? text2 : text1;
}
</script>
<body>
<a onclick="swapText('optionslink','Options >>','Hide Options >>');" id="optionslink">Options >></a>
</body>
</html>