neil1967
01-05-2011, 05:20 PM
Hello All,
I'm new to javascript and trying to build a news page that has expandable/collapsible headlines. I copied some javascript to do this that works fine except that when I try to replace the clickable '+' or '-' characters with clickable images, somehow doing a comparison of the innerHTML in order to toggle it from the plus image to the minus image doesn't work. I'm assuming it has to do with how innerHTML translates tagged html vs. straight characters or text, but have tried everything and can't fix it. Any help would be greatly appreciated. Below are the script and the html in the body.
Here's the script:
<style type="text/css"><!--
.save{
behavior:url(#default#savehistory);}
a.dsphead{
text-decoration:none;}
a.dsphead:hover{
text-decoration:underline;}
a.dsphead span.dspimg{
font-weight:normal;}
.dspcont{
display:none;}
//--></style>
<script type="text/javascript"><!--
function dsp(loc){
if(document.getElementById){
var foc=loc.firstChild;
foc=loc.firstChild.innerHTML?
loc.firstChild:
loc.firstChild.nextSibling;
// this is the problem area, which worked when it was like this:
// foc.innerHTML=foc.innerHTML=='+'?'-':'+';
foc.innerHTML=foc.innerHTML=='<img src=../img/art/expand.png></img>'?'<img src=../img/art/collapse.png></img>':'src=../img/art/expand.png></img>';
foc=loc.parentNode.nextSibling.style?
loc.parentNode.nextSibling:
loc.parentNode.nextSibling.nextSibling;
foc.style.display=foc.style.display=='block'?'none':'block';
}}
if(!document.getElementById)
document.write('<style type="text/css"><!--\n'+
'.dspcont{display:block;}\n'+
'//--></style>');
//--></script>
<noscript>
<style type="text/css"><!--
.dspcont{display:block;}
//--></style>
</noscript>
And here is the html:
<h3><a href="javascript:void(0)" class="dsphead"
onclick="dsp(this)">
<!-- this is the problem area which worked when it was like this:
<span class="dspimg">+</span>
-->
<span class="dspimg"><img src=../img/art/expand.png></img></span>August 2, 2010 - Latest Headline</a></h3>
<div class="dspcont"><br /><p>Content paragraph 1</p><p>Content paragraph 2</p>
I'm new to javascript and trying to build a news page that has expandable/collapsible headlines. I copied some javascript to do this that works fine except that when I try to replace the clickable '+' or '-' characters with clickable images, somehow doing a comparison of the innerHTML in order to toggle it from the plus image to the minus image doesn't work. I'm assuming it has to do with how innerHTML translates tagged html vs. straight characters or text, but have tried everything and can't fix it. Any help would be greatly appreciated. Below are the script and the html in the body.
Here's the script:
<style type="text/css"><!--
.save{
behavior:url(#default#savehistory);}
a.dsphead{
text-decoration:none;}
a.dsphead:hover{
text-decoration:underline;}
a.dsphead span.dspimg{
font-weight:normal;}
.dspcont{
display:none;}
//--></style>
<script type="text/javascript"><!--
function dsp(loc){
if(document.getElementById){
var foc=loc.firstChild;
foc=loc.firstChild.innerHTML?
loc.firstChild:
loc.firstChild.nextSibling;
// this is the problem area, which worked when it was like this:
// foc.innerHTML=foc.innerHTML=='+'?'-':'+';
foc.innerHTML=foc.innerHTML=='<img src=../img/art/expand.png></img>'?'<img src=../img/art/collapse.png></img>':'src=../img/art/expand.png></img>';
foc=loc.parentNode.nextSibling.style?
loc.parentNode.nextSibling:
loc.parentNode.nextSibling.nextSibling;
foc.style.display=foc.style.display=='block'?'none':'block';
}}
if(!document.getElementById)
document.write('<style type="text/css"><!--\n'+
'.dspcont{display:block;}\n'+
'//--></style>');
//--></script>
<noscript>
<style type="text/css"><!--
.dspcont{display:block;}
//--></style>
</noscript>
And here is the html:
<h3><a href="javascript:void(0)" class="dsphead"
onclick="dsp(this)">
<!-- this is the problem area which worked when it was like this:
<span class="dspimg">+</span>
-->
<span class="dspimg"><img src=../img/art/expand.png></img></span>August 2, 2010 - Latest Headline</a></h3>
<div class="dspcont"><br /><p>Content paragraph 1</p><p>Content paragraph 2</p>