Search code examples
javascripttextexpand

How to modify this javascript code to only expand the hidden text


I want to expand hidden text on a webpage and after some (re)search on google i came across the following code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Test</title>


<script type="text/javascript">
function Expand(node)
{
  // Change the image (if there is an image)
  if (node.childNodes.length > 0)
  {
    if (node.firstChild.tagName == "IMG")
    {
      node.firstChild.src = "minus.gif";
    }
  }
  node.nextSibling.style.display = '';
}

function Collapse(node)
{
  // Change the image (if there is an image)
  if (node.childNodes.length > 0)
  {
    if (node.firstChild.tagName == "IMG")
    {
      node.firstChild.src = "plus.gif";
    }
  }
 node.nextSibling.style.display = 'none';
}

function Toggle(node)
{
  // Unfold the branch if it isn't visible
  if (node.nextSibling.style.display == 'none')
  {        
      Expand(node);
  }
  // Collapse the branch if it IS visible
  else
  {        
     Collapse(node);
  }
node.childNodes[1].nodeValue = (node.nextSibling.style.display == 'none')? 'More...' : 'Less...';
}           
</script> 

</head>


<body>


<a onclick="Toggle(this)" style="cursor:pointer;"><img src="plus.gif" alt="Expand/Collapse" /> More...</a><p style="display:none;">This is a sample of the expanded text that will show up when "+ More..." is clicked</p>              


</body>
</html>

Now the script displays a .gif image of a plus sign(expand) with 'More...' beside it and when the .gif file or the 'More...' is clicked the hidden text appears and the plus.gif is replaced with a minus.gif and 'More...' changes to 'Less...' but i want to implement it in another way, i want that once the expand (plus.gif) is clicked, no other .gif should appear again, i dont know how to modify the code to do it, so friends please help me out

i am a newbie in javascript so such a dumb doubt had to come

Thanx :)


Solution

  • Just change your code to this

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Test</title>
            <script type="text/javascript">
    
                function ExpandDelete(node)
                {
                    if (node.nextSibling.style.display == 'none')
                    {        
                        node.nextSibling.style.display = '';
                        node.parentNode.removeChild(node);
                    }
                }           
            </script> 
        </head>
    
        <body>
            <a onclick="ExpandDelete(this);" style="cursor:pointer;"><img src="plus.gif" alt="Expand" /> More...</a><p style="display:none;">This is a sample of the expanded text that will show up when "+ More..." is clicked</p>              
        </body>
    </html>