Félix Desmeules
Félix Desmeules

Reputation: 49

Modify text.innerHTML to an image in show/hide script

I recently got this simple script to show/hide several sections of an html page. I'm using it to show/hide the content of a div by clicking small "+" and "-":

    function toggle1() {
    var ele = document.getElementById("toggleText1");
    var text = document.getElementById("displayText1");
    if(ele.style.display == "block") {
            ele.style.display = "none";
        text.innerHTML = "+";
    }
    else {
        ele.style.display = "block";
        text.innerHTML = "-";
    }
} 

Along with:

<a href="javascript:toggle1();" class="txt_side_table_cmd" id="displayText1><img src="imgs/up.gif" /></a>
<div id="toggleText1" style="display: block">Content here</div>

The code works fine but i'm trying to change the "+" and "-" text links to images links. I have very little knowlege of Javascript and I tried various modifications that made it worst. This original script in in an extrenal .js file.

Any ideas as how to change text links to image links ?

Thank you very much

Upvotes: 0

Views: 1678

Answers (2)

Evan
Evan

Reputation: 825

If I'm understanding correctly, you could make your image links in advance, then just append them to the element where the '+' and '-' were before:

var showControlImage = new Image(),
    hideControlImage = new Image();

showControlImage.src = "path/to/showImage.png";
hideControlImage.src = "path/to/hideImage.png";

// Then just use your toggle function with minor changes:
function toggle1() {
    var ele = document.getElementById("toggleText1");
    var text = document.getElementById("displayText1");
    if(ele.style.display == "block") {
        ele.style.display = "none";
        text.innerHTML = "";
        text.appendChild = showControlImage;
    }
    else {
        ele.style.display = "block";
        text.innerHTML = "";
        text.appendChild = hideControlImage;
    }
} 

Also, you should clean up your tag as per Chandan's suggestion.

Upvotes: 0

C M
C M

Reputation: 703

Your id value is not close properly. Because of that document.getElementById("displayText1") is undefined. so try this

<a href="javascript:toggle1();" class="txt_side_table_cmd" id="displayText1">

instead of your

<a href="javascript:toggle1();" class="txt_side_table_cmd" id="displayText1>

Hope this helps...

Upvotes: 1

Related Questions