Lasse wrote:
I have done this simple function, it seems to work as intended, to
solve a problem i have had for a while. I couldnt find any sample
around that was working for me.
I would like to test it with you and see if there are any improvments
that i should make ;-)
It should be fast and if possible compatible with todays modern
browser-standards. It should be activated by the onload-event.
This is my code, free for all to use:
<html><head>
<title>brTest</title>
</head>
<body>
<br>
<div id=mybrid>
<a id=myid1 href="#">Text 1 with<br>a linebreak (<br>)</a>
<br clear="all">
<a id=myid2 href="#">Text 2 with<br>a linebreak (<br>)</a>
</div>
<br>
<a id=myid3 href="#">Text 3 with<br>a linebreak (<br>) that
should remain as it is.</a>
<br><br>
<SCRIPT LANGUAGE="JavaScript"><!--
function brTest(){
var d=document.getElementById;
d("mybrid").innerHTML=d("mybrid").innerHTML.replac e(/<br>/gim," ");
}
//--></SCRIPT>
<form>
<button onclick="brTest();">Call brTest()</button>
</form>
</body>
</html>
Setting innerHTML means the browser needs to reparse the complete HTML
content of the HTML and render it. If all you want is to replace some
<br> elements then you can use the DOM (unless you want to cover IE4 but
your solution doesn't do it with the use of document.getElementById):
<html>
<head>
<title>replacing <br> elements</title>
<script type="text/javascript">
function replaceBRs (elementId, replacementText) {
var element, brs;
if (document.getElementById) {
element = document.getElementById(elementId);
if (element && element.getElementsByTagName &&
document.createTextNode) {
brs = element.getElementsByTagName('br');
while (brs.length) {
var br = brs[brs.length - 1];
var replacement = document.createTextNode(replacementText);
br.parentNode.replaceChild(replacement, br);
}
}
}
}
</script>
</head>
<body>
<p>
<input type="button" value="replace brs"
onclick="replaceBRs('testDiv', String.fromCharCode(160));">
</p>
<div id="testDiv">
<a href="http://JavaScript.faqts.com/">
JavaScript FAQTs
<br>
JavaScript Questions and Answers
</a>
<br>
<p>
All for Kibology.
<br>
Kibology for all.
</p>
</div>
</body>
</html>
--
Martin Honnen
http://JavaScript.FAQTs.com/