r/HTML • u/AppointmentWild3978 • Jul 22 '26
Question How do you make an onclick that changes back to the original text when clicked again.
<html>
<body>
<p id="demo">**File Number 1**.</p>
<script> document.getElementById("demo").addEventListener("click", myFunction); function myFunction() { document.getElementById("demo").innerHTML = "**censored file information here**"; } </script>
</body>
</html>
Im using this code to make it so that when I click on "File Number 1", it shows the file information. It works, however, I want it to be so that when you click on the file information, it returns to just saying "file number 1", and you can do that infinitely. How would I do this? thanks
3
u/MineDesperate8982 Jul 22 '26
Like this? https://jsfiddle.net/3v72bwus/3
But the <summary> solution u/cryothic proposed is also pretty neat.
- Or you could render both variants of the text and hide/unhide the appropriate element on click: https://www.w3schools.com/howto/howto_js_toggle_hide_show.asp
- Or you could assign a key to the value
```<span class="file" id="file-1" data-key="aV8nJ">** File Number 1**<span>```
And, on click, execute an ajax to fetch from an internal db the file data based on the data-key value.
Depending no how many things you want to take in to consideration, you could do pretty neat stuff.
For example, you have multiple "switches" for different files. Wouldn't it be a good idea to also default the value of a field whenever a different field is clicked? Like, we have two items:
```
<span class="file" id="file-1" data-key="aV8nJ">** File Number 1**<span>
<span class="file" id="file-2" data-key="u90JW">** File Number 2**<span>
```
You click on the first file, it shows the data you want to, and change it back whenever the user either clicks on "#file-1", or outside of it, or on "#file-2".
2
u/reznaeous Jul 22 '26
Another approach would be to have the file info as a hidden element, and use a bit of JS to toggle which element is visible. Here's a CodePen demo showing a way to do this.
2
u/tonypconway Jul 22 '26 edited Jul 22 '26
You've got two options:
I replaced your broken out function with an arrow function because it's my preference.
Both of those work well enough. If you want to scale this up to multiple elements on the same page without having to repeat code, look at using `e.srcElement` in your JS and adding data in attributes, or using unique IDs to correspond to a data object in JS.
EDIT: formatting
7
u/cryothic Jul 22 '26
Take a look at the <summary> element.
<summary> HTML disclosure summary element - HTML | MDN
I think that does exactly what you want, without any javascript.
4
u/armahillo Expert Jul 22 '26
Here's a way to do it without JS
https://codepen.io/armahillo/pen/RNKBmbP
I added comments to explain the various lines.
Try to look for non-JS way to do things. I see a lot of newer webdevs learn JS and then stop learning. It can be a little more challenging, but will make you a stronger dev.
Also, it's worth noting that all client-side web source is exposed to the user. Don't put any actually sensitive information. This show/hide behavior should be considered a convenience to the user, not a security practice.
2
u/AdamantiteM Jul 22 '26
If you don't want to use the <summary> element like u/cryothic said, you would need to store the original text un a variable when you execute the function. The variable has to be out of the function's scope otherwise it'll get re-assigned each time you execute the function
2
u/hightrix Jul 22 '26
Keep it simple, stupid.
Make two text fields “demo” and “demo shown”. When you click one, hide it and show the other.