If the details
tag has an open
attribute, the summary
text will say "Close".
My attempt at changing the text:
if (jQuery("details").click().attr("open")) {
jQuery("summary").text("Close");
} else if (jQuery("details").click().attr("")) {
jQuery("summary").text("Show")
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<details class="description" open>
<summary class="">Show</summary>
<p class="">Description text</p>
</details>
If you want to edit the text after page load, I would recommend just using jQuery: $('. mydetails summary'). html('Change the text to this');
Definition and Usage The <summary> tag defines a visible heading for the <details> element. The heading can be clicked to view/hide the details. Note: The <summary> element should be the first child element of the <details> element.
The <summary> element in HTML is used to specify a visible heading, summary, caption, or legend for the <details> element's interactive box. Users click on the visible text wrapped by the <summary> element, which can open or close the box that contains the markup of the children elements of the <details> element.
Definition and Usage The <details> tag specifies additional details that the user can open and close on demand. The <details> tag is often used to create an interactive widget that the user can open and close. By default, the widget is closed.
Simply use CSS for this, here combined with a neat fading/grow/shrink effect:
details.description summary::after {
content: attr(data-open);
opacity: 0;
font-size: 5px;
}
details.description[open] summary::after {
content: attr(data-open);
opacity: 1;
font-size: 14px;
}
details.description summary::before {
content: attr(data-close);
opacity: 0;
font-size: 5px;
}
details.description:not([open]) summary::before {
content: attr(data-close);
opacity: 1;
font-size: 14px;
}
details.description summary::after,
details.description summary::before {
display: inline-block;
transition: all .4s ease-in-out;
transform-origin: center bottom;
}
<details class="description" open>
<summary data-open="Close" data-close="Show"></summary>
<p>Description text</p>
</details>
Here's the minimal version:
details.description[open] summary::after {
content: attr(data-open);
}
details.description:not([open]) summary::after {
content: attr(data-close);
}
<details class="description" open>
<summary data-open="Close" data-close="Show"></summary>
<p>Description text</p>
</details>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With