Widget talk:Expand or Collapse All

From Kicksecure
Jump to navigation Jump to search

This should fix it. Round about way

<button onclick="expand()">Expand all</button>
<button onclick="collapse()">Collapse all</button>
<script>
function collapse() {
    var x = document.getElementsByClassName("mw-collapsible-text");
    var i;
    for (i = 0; i < x.length; i++) {
    if ( x[i].innerHTML == "Collapse" ) {
        x[i].click(); }
    }
}
function expand() {
    var x = document.getElementsByClassName("mw-collapsible-text");
    var i;
    for (i = 0; i < x.length; i++) {
    if ( x[i].innerHTML == "Expand" ) {
        x[i].click(); }
    }
}
</script>
Hmm, on second thought, probably better to use mw.message and fetch it in plain() so the pages MediaWiki:Collapsible-collapse and MediaWiki:Collapsible-expand can be tweaked. eg. Show instead of Expand and Hide instead of Collapse. This way it is less likely to break if someone changes those message values. For reference I did try an array with childNode but it was not reliable enough on the click() event.
<button onclick="expand()">Expand all</button>
<button onclick="collapse()">Collapse all</button>
<script>
function collapse() {
    var x = document.getElementsByClassName("mw-collapsible-text");
    var i;
    for (i = 0; i < x.length; i++) {
    if ( x[i].innerHTML == mw.message( 'collapsible-collapse' ).plain() ) {
        x[i].click(); }
    }
}
function expand() {
    var x = document.getElementsByClassName("mw-collapsible-text");
    var i;
    for (i = 0; i < x.length; i++) {
    if ( x[i].innerHTML == mw.message( 'collapsible-expand' ).plain() ) {
        x[i].click(); }
    }
}
</script>