Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
•
Sichere ProgrammierungSecurity: Mehrere Probleme in python-tornado (SUSE)(01.10.2026 um 00:21 Uhr)
•
Sichere ProgrammierungSecurity: Ausführen beliebiger Kommandos in python-PyYAML (SUSE)(01.10.2026 um 00:21 Uhr)
•
Unix & Linux ServerSecurity: Ausführen beliebiger Kommandos in Kdenlive (Ubuntu)(01.10.2026 um 00:21 Uhr)
•
Unix & Linux ServerSecurity: Ausführen beliebiger Kommandos in AuthenSASL (Ubuntu)(01.10.2026 um 00:56 Uhr)
•
Unix & Linux ServerSecurity: Zwei Probleme in dogtag-pki (Red Hat)(01.10.2026 um 01:00 Uhr)
•
Unix & Linux ServerSecurity: Zwei Probleme in pki-core (Red Hat)(01.10.2026 um 01:00 Uhr)
•
Unix & Linux ServerSecurity: Zwei Probleme in gvfs (Red Hat)(01.10.2026 um 01:00 Uhr)
•
Unix & Linux ServerSecurity: Denial of Service in jawn (SUSE)(01.10.2026 um 01:00 Uhr)
•
Sicherheitslücken (CVE)USN-8845-1: GVfs vulnerabilities(30.09.2026 um 17:54 Uhr)
••
Sichere ProgrammierungSecurity: Mehrere Probleme in python-tornado (SUSE)(01.10.2026 um 00:21 Uhr)
•
Sichere ProgrammierungSecurity: Ausführen beliebiger Kommandos in python-PyYAML (SUSE)(01.10.2026 um 00:21 Uhr)
•
Unix & Linux ServerSecurity: Ausführen beliebiger Kommandos in Kdenlive (Ubuntu)(01.10.2026 um 00:21 Uhr)
•
Unix & Linux ServerSecurity: Ausführen beliebiger Kommandos in AuthenSASL (Ubuntu)(01.10.2026 um 00:56 Uhr)
•
Unix & Linux ServerSecurity: Zwei Probleme in dogtag-pki (Red Hat)(01.10.2026 um 01:00 Uhr)
•
Unix & Linux ServerSecurity: Zwei Probleme in pki-core (Red Hat)(01.10.2026 um 01:00 Uhr)
•
Unix & Linux ServerSecurity: Zwei Probleme in gvfs (Red Hat)(01.10.2026 um 01:00 Uhr)
•
Unix & Linux ServerSecurity: Denial of Service in jawn (SUSE)(01.10.2026 um 01:00 Uhr)
•
Sicherheitslücken (CVE)USN-8845-1: GVfs vulnerabilities(30.09.2026 um 17:54 Uhr)
•
Intelligence View
⚡ tsecurity.de Intelligence

Fixing a "Show More / Show Less" Button with JavaScript

Hello developers! 👨‍💻👩‍💻 I'm a CS student and im learning javascript because i want to be a frontend developer , and today I wanted to share a concept I recentl…

Beitrag
0
Seite
0
↗ Quelle (dev.to)
Social ReaktionenReagiere als Erste:r — dein Feedback zählt!

Hello developers! 👨‍💻👩‍💻

I'm a CS student and im learning javascript because i want to be a frontend developer , and today I wanted to share a concept I recently learned while making a simple "Show More / Show Less" button using JavaScript.

can you tell what the diffrence between the first code and the second

and witch one is true !!



I was trying to create a button that toggles the visibility of text, like this:

show more



show less



simple html 2 paragrah one of them with the class of text hidden-text

it the one we will toggle and a button that will do the job




<p>lorem ipsum dolor sit amet consectetur adipisicing elit. </p>
<p class="text hidden-text">fsdfsfsd .... </p>
<button class="show-more">show more....</button>







css:




.hidden-text {
display: none;
}






ive declared all the variable i need




let showButton = document.querySelector(".show-more");
let text = document.querySelector(".text");
let value = false; // Initially, the text is hidden







my first code was :




showButton.addEventListener("click", (value) => {
value = !value;
text.classList.toggle("hidden-text");
showButton.textContent = value ? "show less..." : "show more..."; // Update button text
});






but this didn't work the value was always false even that i swithced using value=!value

after some search i found out why this happend



Every time the button was clicked, the local value parameter inside the callback function was being redeclared, which shadows the outer value.



that means that The outer value remains unchanged, always staying false.

and The local value is toggled inside the callback, but it only exists during that function's execution and doesn’t affect the outer value.



To solve this, I needed to remove the value parameter from the callback and directly use the outer value that persists across button clicks.




let value = false; // Persistent state variable

showButton.addEventListener("click", () => {
value = !value; // Toggle the outer value
text.classList.toggle("hidden-text");
showButton.textContent = value ? "show less..." : "show more..."; // Update button text based on value
});



Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Fixing a "Show More / Show Less" Button with JavaScript

Thematisch verwandte Begriffe: Fixing, Show, More, Less · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
tsecurity.de Icon
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag