Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Button to toggle fullscreen works only for first element

Solved by Karmalakas View solution

Started by Sebastian 5 years ago · 2 replies · 415 views
5 years ago

Hi,

I am trying to build a webpage for my class with exercises and projects and I want these to have a button, with which one can toggle them into "fullscreen"-mode. I have already managed to add this button and it works, but it only works for the first project (or exercise) on a page. If I click on the button of the second project, the site jumps to the top and nothing else happens.

Is there someone, who could help me?

HTML / Markdown on the page:

HTML
<div markdown="1" class="projekt">
#### Fußgängerampel
<div class="projekt-actions">
    <a href="#" id="projekt-fullscreen" role="button" title="Erweitern"><i class="fa fa-expand"></i></a>
</div>

Baue und programmiere eine Fußgängerampel! Nutze dazu die Informationen aus dem Info-Kasten zum Taster.
</div>

<div markdown="1" class="projekt">
#### Juke-Box
<div class="projekt-actions">
    <a href="#" id="projekt-fullscreen" role="button" title="Erweitern"><i class="fa fa-expand"></i></a>
</div>

Baue und programmiere eine Juke-Box!
</div>

CSS:

CSS
/* toggle class 'aufgabe' and 'projekt' to fullscreen */
.aufgabe-actions, .projekt-actions {
  float: right;
  /*margin-left: auto;
  margin-right: 0;
  display: inline-block;
  text-align: right;*/
}

.aufgabe-fullscreen {
    display: block;
    z-index: 9999;
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    margin-top: 0em;
    margin-bottom: 0em;
    overflow: auto;
    padding-left: 10%;
    padding-right: 10%;
    padding-top: 2em;
}

.projekt-fullscreen {
    display: block;
    z-index: 9999;
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    margin-top: 0em;
    margin-bottom: 0em;
    overflow: auto;
    padding-left: 10%;
    padding-right: 10%;
    padding-top: 2em;
    border-left: 35px solid #007f61;
}

Javascript:

JAVASCRIPT
$(document).ready(function () {
    //Toggle fullscreen
    $("#aufgabe-fullscreen").click(function (e) {
        e.preventDefault();

        var $this = $(this);

        if ($this.children('i').hasClass('fa-expand'))
        {
            $this.children('i').removeClass('fa-expand');
            $this.children('i').addClass('fa-compress');
        }
        else if ($this.children('i').hasClass('fa-compress'))
        {
            $this.children('i').removeClass('fa-compress');
            $this.children('i').addClass('fa-expand');
        }
        $(this).closest('.aufgabe').toggleClass('aufgabe-fullscreen');
    });
    $("#projekt-fullscreen").click(function (e) {
        e.preventDefault();

        var $this = $(this);

        if ($this.children('i').hasClass('fa-expand'))
        {
            $this.children('i').removeClass('fa-expand');
            $this.children('i').addClass('fa-compress');
        }
        else if ($this.children('i').hasClass('fa-compress'))
        {
            $this.children('i').removeClass('fa-compress');
            $this.children('i').addClass('fa-expand');
        }
        $(this).closest('.projekt').toggleClass('projekt-fullscreen');
    })
});
5 years ago Solution

You can't have multiple elements with same ID. Change IDs to be different (or remove if not used elsewhere) and target selector by class name. Eg.:

HTML
<a href="#" class="projekt-fullscreen" role="button" title="Erweitern"></a>
<...>
$("a.projekt-fullscreen").click(function (e) {}
👍 1

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
3 90 1 hour ago
Support · by Anna, 3 days ago
2 92 22 hours ago
Support · by Justin Young, 23 hours ago
1 40 23 hours ago
Support · by Duc , 1 week ago
2 98 6 days ago
Support · by Colin Hume, 1 week ago
2 90 6 days ago