Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change <summary> text if <details> has 'open' attr

Tags:

html

jquery

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>
like image 266
Nick0989 Avatar asked Mar 06 '19 20:03

Nick0989


People also ask

How do I change my details tag name?

If you want to edit the text after page load, I would recommend just using jQuery: $('. mydetails summary'). html('Change the text to this');

What is the use of details and summary tag?

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.

How do you write a summary in HTML?

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.

When would you use the details element in your website and why?

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.


1 Answers

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>
like image 129
connexo Avatar answered Nov 03 '22 01:11

connexo