Question

Here's my code:

<script>
    function toggle_visibility(id) {
        var e = document.getElementById(id);
        if (e.style.display == 'none') {
            document.getElementById('id + x').innerText = '[-]';
            e.style.display = 'block';
        }
        else {
            document.getElementById('id + x').innerText = '[+]';
            e.style.display = 'none';
        }
    }
</script>

<a href="#" onclick="toggle_visibility(httpserver);" id="httpserverx">[+]</a>
<div id="httpserver" style="display:block;">
....my content...
</div>

When I click the + nothing happens. I will eventually have multiple divs, each specifically named, so this function needs to be able to handle all the divs.

Any help is appreciated!

Était-ce utile?

La solution

First you have to change your HTML. You have to add "" when you pass the value.

Change

onclick="toggle_visibility(httpserver);"

to

onclick="toggle_visibility('httpserver');"

HTML:

<a href="#" onclick="toggle_visibility('httpserver');" id="httpserverx">[+]</a>
<div id="httpserver" style="display:none;">
....my content...
</div>

And then change

document.getElementById('id + x').innerText

to

document.getElementById(id + 'x').innerText

You should pass the var id appended with the char x but you pass the id+x as a char

JS:

function toggle_visibility(id) {
        var e = document.getElementById(id);
        if (e.style.display == 'none') {
            document.getElementById(id + 'x').innerText = '[-]';
            e.style.display = 'block';
        }
        else {
            document.getElementById(id + 'x').innerText = '[+]';
            e.style.display = 'none';
        }
    }

DEMO

Autres conseils

I think you need this:

First you need to change markup with following

onclick="toggle_visibility('httpserver');" // Wrap id with ''

As there was some quotes error in js too look for the comments in the below:

function toggle_visibility(id) {
    var e = document.getElementById(id);

    if (e.style.display == 'none') {
        document.getElementById(id + 'x').innerText = '[-]'; // Miss placed quotes ' with id and x
        e.style.display = 'block';
    } else {
        document.getElementById(id + 'x').innerText = '[+]'; // Miss placed quotes ' with id and x
        e.style.display = 'none'; 
    }
}

Fiddle

You can accomplish this in a more concise and easier fashion using CSS and simply toggling a class via JS. Css content can swap out the + or - as well as show/hide the relevant contents.

.collapsible .toggle:before { content: '[+]' }
.collapsible.opened .toggle:before { content: '[-]' }
.collapsible .contents { display: none }
.collapsible.opened .contents { display: block }

$('.collapsible').on('click','.toggle',function() {
  $(this).parent().toggleClass('opened'); 
});

<section class="collapsible"><a class="toggle" data-contents="part1">Part1</a><div class="contents" id="contents-part1">This is the expand/collapse content.</div></section>
<section class="collapsible"><a class="toggle" data-contents="part2">Part2</a><div class="contents" id="contents-part2">This is the expand/collapse content.</div></section>

http://jsfiddle.net/YG3kK/

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top