Added content to fixed height make it not viewable, and using null height break animation

I have a button that should hidden area with nice animation. It’s using height attribute to make the animation.

The issue is, when I add content to the “hiddenable” area, the content isn’t viewable as it’s outside the given height. And I have to clic again on “Open” to re-hide and re-open the area.

Here is a MRE :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function changeVisility(all, toCancel = null) {
if(toCancel != null)
toCancel.preventDefault();
if(!(Symbol.iterator in Object(all)))
all = [ all ];
for(var div of all) {
for(const wrapper of div.querySelectorAll(".collapse-wrapper")) {
if(wrapper.style.height == '0px') {
wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`;
} else {
wrapper.style.height = '0px';
}
}
}
}
for(const wrapper of document.querySelectorAll(".collapse-wrapper")) {
if(wrapper.style.height != '0px') { // set real height
wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`;
} else
wrapper.style.height = '0px';
}
function addContent() {
document.getElementById("contentToAdd").insertAdjacentHTML("beforeend", "<p>Other text</p>");
}</code>
<code>function changeVisility(all, toCancel = null) { if(toCancel != null) toCancel.preventDefault(); if(!(Symbol.iterator in Object(all))) all = [ all ]; for(var div of all) { for(const wrapper of div.querySelectorAll(".collapse-wrapper")) { if(wrapper.style.height == '0px') { wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`; } else { wrapper.style.height = '0px'; } } } } for(const wrapper of document.querySelectorAll(".collapse-wrapper")) { if(wrapper.style.height != '0px') { // set real height wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`; } else wrapper.style.height = '0px'; } function addContent() { document.getElementById("contentToAdd").insertAdjacentHTML("beforeend", "<p>Other text</p>"); }</code>
function changeVisility(all, toCancel = null) {
    if(toCancel != null)
        toCancel.preventDefault();
    if(!(Symbol.iterator in Object(all)))
        all = [ all ];
    for(var div of all) {
        for(const wrapper of div.querySelectorAll(".collapse-wrapper")) {
            if(wrapper.style.height == '0px') {
                wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`;
            } else {
                wrapper.style.height = '0px';
            }
        }
    }
}

for(const wrapper of document.querySelectorAll(".collapse-wrapper")) {
    if(wrapper.style.height != '0px') { // set real height
        wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`;
    } else
        wrapper.style.height = '0px';
}

function addContent() {
    document.getElementById("contentToAdd").insertAdjacentHTML("beforeend", "<p>Other text</p>");
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.collapse-wrapper {
overflow: hidden;
transition: height 300ms ease-in;
}</code>
<code>.collapse-wrapper { overflow: hidden; transition: height 300ms ease-in; }</code>
.collapse-wrapper {
    overflow: hidden;
    transition: height 300ms ease-in;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<body style="margin-left: 10px;">
<div class="form-check" style="padding-top: 0.5rem;">
<input class="form-check-input" type="checkbox" name="newinput" id="newinput" onchange="changeVisility(document.getElementById('input_selection'));">
<label class="form-check-label" for="newinput">
Open
</label>
</div>
<div id="input_selection">
<div style="height: 0px;" class="collapse-wrapper">
<div class="collapse-content row" id="contentToAdd" style="background-color: red;">
<button onclick="addContent()" type="button" class="btn btn-primary">Add</button>
<p>Some text</p>
</div>
</div>
</div>
</body></code>
<code><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"> <body style="margin-left: 10px;"> <div class="form-check" style="padding-top: 0.5rem;"> <input class="form-check-input" type="checkbox" name="newinput" id="newinput" onchange="changeVisility(document.getElementById('input_selection'));"> <label class="form-check-label" for="newinput"> Open </label> </div> <div id="input_selection"> <div style="height: 0px;" class="collapse-wrapper"> <div class="collapse-content row" id="contentToAdd" style="background-color: red;"> <button onclick="addContent()" type="button" class="btn btn-primary">Add</button> <p>Some text</p> </div> </div> </div> </body></code>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">

<body style="margin-left: 10px;">
<div class="form-check" style="padding-top: 0.5rem;">
    <input class="form-check-input" type="checkbox" name="newinput" id="newinput" onchange="changeVisility(document.getElementById('input_selection'));">
    <label class="form-check-label" for="newinput">
        Open
    </label>
</div>
<div id="input_selection">
    <div style="height: 0px;" class="collapse-wrapper">
        <div class="collapse-content row" id="contentToAdd" style="background-color: red;">
            <button onclick="addContent()" type="button" class="btn btn-primary">Add</button>
            <p>Some text</p>
        </div>
    </div>
</div>
</body>

To reproduce :

  1. Clic on “Open” to open the area
  2. Clic on “Add” – Nothing show (but the text is well added to HTML)
  3. Clic on “Open” twice (to hide then re-open)
  4. See the red area upgrade with “Other text” showed

How can I make them be showable and keeping the nice animation ?

I tried to set height to null to show, but there is no longer animation…

7

Everytime content is added you need to recalculate the new height. addContent(e) has been changed:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function addContent(e) {
this.parentElement.insertAdjacentHTML("beforeend", "<p>Other text</p>");
const wrapper = this.closest(".collapse-wrapper");
wrapper.style.height = wrapper.querySelector(".collapse-content")
.getBoundingClientRect().height + `px`;
}
</code>
<code>function addContent(e) { this.parentElement.insertAdjacentHTML("beforeend", "<p>Other text</p>"); const wrapper = this.closest(".collapse-wrapper"); wrapper.style.height = wrapper.querySelector(".collapse-content") .getBoundingClientRect().height + `px`; } </code>
function addContent(e) {
  this.parentElement.insertAdjacentHTML("beforeend", "<p>Other text</p>");
  const wrapper = this.closest(".collapse-wrapper");
  wrapper.style.height = wrapper.querySelector(".collapse-content")
    .getBoundingClientRect().height + `px`;
}

In fact I changed everything so it’s no longer dependent on #ids thus allowing you to add as many dropdowns as you like. Every <input> needs class="toggle" and every <button> needs class="add".

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function changeVisility(e) {
const wrapper = this.closest(".form-check").nextElementSibling.querySelector(".collapse-wrapper");
if (wrapper.style.height == '0px') {
wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`;
} else {
wrapper.style.height = '0px';
}
}
function addContent(e) {
this.parentElement.insertAdjacentHTML("beforeend", "<p>Other text</p>");
const wrapper = this.closest(".collapse-wrapper");
wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`;
}
document.querySelectorAll(".toggle").forEach(t => t.onchange = changeVisility);
document.querySelectorAll(".add").forEach(a => a.onclick = addContent);</code>
<code>function changeVisility(e) { const wrapper = this.closest(".form-check").nextElementSibling.querySelector(".collapse-wrapper"); if (wrapper.style.height == '0px') { wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`; } else { wrapper.style.height = '0px'; } } function addContent(e) { this.parentElement.insertAdjacentHTML("beforeend", "<p>Other text</p>"); const wrapper = this.closest(".collapse-wrapper"); wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`; } document.querySelectorAll(".toggle").forEach(t => t.onchange = changeVisility); document.querySelectorAll(".add").forEach(a => a.onclick = addContent);</code>
function changeVisility(e) {
  const wrapper = this.closest(".form-check").nextElementSibling.querySelector(".collapse-wrapper");
  if (wrapper.style.height == '0px') {
    wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`;
  } else {
    wrapper.style.height = '0px';
  }
}

function addContent(e) {
  this.parentElement.insertAdjacentHTML("beforeend", "<p>Other text</p>");
  const wrapper = this.closest(".collapse-wrapper");
  wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`;
}

document.querySelectorAll(".toggle").forEach(t => t.onchange = changeVisility);

document.querySelectorAll(".add").forEach(a => a.onclick = addContent);
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.collapse-wrapper {
overflow: hidden;
transition: height 300ms ease-in;
}</code>
<code>.collapse-wrapper { overflow: hidden; transition: height 300ms ease-in; }</code>
.collapse-wrapper {
  overflow: hidden;
  transition: height 300ms ease-in;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<body style="margin-left: 10px;">
<div class="form-check" style="padding-top: 0.5rem;">
<label class="form-check-label">
<input class="toggle form-check-input" type="checkbox" name="newinput">
Open
</label>
</div>
<div id="input_selection">
<div style="height: 0px;" class="collapse-wrapper">
<div class="collapse-content row" style="background-color: red;">
<button type="button" class="add btn btn-primary">Add</button>
<p>Some text</p>
</div>
</div>
</div>
</body></code>
<code><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"> <body style="margin-left: 10px;"> <div class="form-check" style="padding-top: 0.5rem;"> <label class="form-check-label"> <input class="toggle form-check-input" type="checkbox" name="newinput"> Open </label> </div> <div id="input_selection"> <div style="height: 0px;" class="collapse-wrapper"> <div class="collapse-content row" style="background-color: red;"> <button type="button" class="add btn btn-primary">Add</button> <p>Some text</p> </div> </div> </div> </body></code>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">

<body style="margin-left: 10px;">
  <div class="form-check" style="padding-top: 0.5rem;">
    <label class="form-check-label">
      <input class="toggle form-check-input" type="checkbox" name="newinput">
      Open
    </label>
  </div>
  <div id="input_selection">
    <div style="height: 0px;" class="collapse-wrapper">
      <div class="collapse-content row" style="background-color: red;">
        <button type="button" class="add btn btn-primary">Add</button>
        <p>Some text</p>
      </div>
    </div>
  </div>
</body>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Added content to fixed height make it not viewable, and using null height break animation

I have a button that should hidden area with nice animation. It’s using height attribute to make the animation.

The issue is, when I add content to the “hiddenable” area, the content isn’t viewable as it’s outside the given height. And I have to clic again on “Open” to re-hide and re-open the area.

Here is a MRE :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function changeVisility(all, toCancel = null) {
if(toCancel != null)
toCancel.preventDefault();
if(!(Symbol.iterator in Object(all)))
all = [ all ];
for(var div of all) {
for(const wrapper of div.querySelectorAll(".collapse-wrapper")) {
if(wrapper.style.height == '0px') {
wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`;
} else {
wrapper.style.height = '0px';
}
}
}
}
for(const wrapper of document.querySelectorAll(".collapse-wrapper")) {
if(wrapper.style.height != '0px') { // set real height
wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`;
} else
wrapper.style.height = '0px';
}
function addContent() {
document.getElementById("contentToAdd").insertAdjacentHTML("beforeend", "<p>Other text</p>");
}</code>
<code>function changeVisility(all, toCancel = null) { if(toCancel != null) toCancel.preventDefault(); if(!(Symbol.iterator in Object(all))) all = [ all ]; for(var div of all) { for(const wrapper of div.querySelectorAll(".collapse-wrapper")) { if(wrapper.style.height == '0px') { wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`; } else { wrapper.style.height = '0px'; } } } } for(const wrapper of document.querySelectorAll(".collapse-wrapper")) { if(wrapper.style.height != '0px') { // set real height wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`; } else wrapper.style.height = '0px'; } function addContent() { document.getElementById("contentToAdd").insertAdjacentHTML("beforeend", "<p>Other text</p>"); }</code>
function changeVisility(all, toCancel = null) {
    if(toCancel != null)
        toCancel.preventDefault();
    if(!(Symbol.iterator in Object(all)))
        all = [ all ];
    for(var div of all) {
        for(const wrapper of div.querySelectorAll(".collapse-wrapper")) {
            if(wrapper.style.height == '0px') {
                wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`;
            } else {
                wrapper.style.height = '0px';
            }
        }
    }
}

for(const wrapper of document.querySelectorAll(".collapse-wrapper")) {
    if(wrapper.style.height != '0px') { // set real height
        wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`;
    } else
        wrapper.style.height = '0px';
}

function addContent() {
    document.getElementById("contentToAdd").insertAdjacentHTML("beforeend", "<p>Other text</p>");
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.collapse-wrapper {
overflow: hidden;
transition: height 300ms ease-in;
}</code>
<code>.collapse-wrapper { overflow: hidden; transition: height 300ms ease-in; }</code>
.collapse-wrapper {
    overflow: hidden;
    transition: height 300ms ease-in;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<body style="margin-left: 10px;">
<div class="form-check" style="padding-top: 0.5rem;">
<input class="form-check-input" type="checkbox" name="newinput" id="newinput" onchange="changeVisility(document.getElementById('input_selection'));">
<label class="form-check-label" for="newinput">
Open
</label>
</div>
<div id="input_selection">
<div style="height: 0px;" class="collapse-wrapper">
<div class="collapse-content row" id="contentToAdd" style="background-color: red;">
<button onclick="addContent()" type="button" class="btn btn-primary">Add</button>
<p>Some text</p>
</div>
</div>
</div>
</body></code>
<code><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"> <body style="margin-left: 10px;"> <div class="form-check" style="padding-top: 0.5rem;"> <input class="form-check-input" type="checkbox" name="newinput" id="newinput" onchange="changeVisility(document.getElementById('input_selection'));"> <label class="form-check-label" for="newinput"> Open </label> </div> <div id="input_selection"> <div style="height: 0px;" class="collapse-wrapper"> <div class="collapse-content row" id="contentToAdd" style="background-color: red;"> <button onclick="addContent()" type="button" class="btn btn-primary">Add</button> <p>Some text</p> </div> </div> </div> </body></code>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">

<body style="margin-left: 10px;">
<div class="form-check" style="padding-top: 0.5rem;">
    <input class="form-check-input" type="checkbox" name="newinput" id="newinput" onchange="changeVisility(document.getElementById('input_selection'));">
    <label class="form-check-label" for="newinput">
        Open
    </label>
</div>
<div id="input_selection">
    <div style="height: 0px;" class="collapse-wrapper">
        <div class="collapse-content row" id="contentToAdd" style="background-color: red;">
            <button onclick="addContent()" type="button" class="btn btn-primary">Add</button>
            <p>Some text</p>
        </div>
    </div>
</div>
</body>

To reproduce :

  1. Clic on “Open” to open the area
  2. Clic on “Add” – Nothing show (but the text is well added to HTML)
  3. Clic on “Open” twice (to hide then re-open)
  4. See the red area upgrade with “Other text” showed

How can I make them be showable and keeping the nice animation ?

I tried to set height to null to show, but there is no longer animation…

7

Everytime content is added you need to recalculate the new height. addContent(e) has been changed:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function addContent(e) {
this.parentElement.insertAdjacentHTML("beforeend", "<p>Other text</p>");
const wrapper = this.closest(".collapse-wrapper");
wrapper.style.height = wrapper.querySelector(".collapse-content")
.getBoundingClientRect().height + `px`;
}
</code>
<code>function addContent(e) { this.parentElement.insertAdjacentHTML("beforeend", "<p>Other text</p>"); const wrapper = this.closest(".collapse-wrapper"); wrapper.style.height = wrapper.querySelector(".collapse-content") .getBoundingClientRect().height + `px`; } </code>
function addContent(e) {
  this.parentElement.insertAdjacentHTML("beforeend", "<p>Other text</p>");
  const wrapper = this.closest(".collapse-wrapper");
  wrapper.style.height = wrapper.querySelector(".collapse-content")
    .getBoundingClientRect().height + `px`;
}

In fact I changed everything so it’s no longer dependent on #ids thus allowing you to add as many dropdowns as you like. Every <input> needs class="toggle" and every <button> needs class="add".

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function changeVisility(e) {
const wrapper = this.closest(".form-check").nextElementSibling.querySelector(".collapse-wrapper");
if (wrapper.style.height == '0px') {
wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`;
} else {
wrapper.style.height = '0px';
}
}
function addContent(e) {
this.parentElement.insertAdjacentHTML("beforeend", "<p>Other text</p>");
const wrapper = this.closest(".collapse-wrapper");
wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`;
}
document.querySelectorAll(".toggle").forEach(t => t.onchange = changeVisility);
document.querySelectorAll(".add").forEach(a => a.onclick = addContent);</code>
<code>function changeVisility(e) { const wrapper = this.closest(".form-check").nextElementSibling.querySelector(".collapse-wrapper"); if (wrapper.style.height == '0px') { wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`; } else { wrapper.style.height = '0px'; } } function addContent(e) { this.parentElement.insertAdjacentHTML("beforeend", "<p>Other text</p>"); const wrapper = this.closest(".collapse-wrapper"); wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`; } document.querySelectorAll(".toggle").forEach(t => t.onchange = changeVisility); document.querySelectorAll(".add").forEach(a => a.onclick = addContent);</code>
function changeVisility(e) {
  const wrapper = this.closest(".form-check").nextElementSibling.querySelector(".collapse-wrapper");
  if (wrapper.style.height == '0px') {
    wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`;
  } else {
    wrapper.style.height = '0px';
  }
}

function addContent(e) {
  this.parentElement.insertAdjacentHTML("beforeend", "<p>Other text</p>");
  const wrapper = this.closest(".collapse-wrapper");
  wrapper.style.height = wrapper.querySelector(".collapse-content").getBoundingClientRect().height + `px`;
}

document.querySelectorAll(".toggle").forEach(t => t.onchange = changeVisility);

document.querySelectorAll(".add").forEach(a => a.onclick = addContent);
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.collapse-wrapper {
overflow: hidden;
transition: height 300ms ease-in;
}</code>
<code>.collapse-wrapper { overflow: hidden; transition: height 300ms ease-in; }</code>
.collapse-wrapper {
  overflow: hidden;
  transition: height 300ms ease-in;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<body style="margin-left: 10px;">
<div class="form-check" style="padding-top: 0.5rem;">
<label class="form-check-label">
<input class="toggle form-check-input" type="checkbox" name="newinput">
Open
</label>
</div>
<div id="input_selection">
<div style="height: 0px;" class="collapse-wrapper">
<div class="collapse-content row" style="background-color: red;">
<button type="button" class="add btn btn-primary">Add</button>
<p>Some text</p>
</div>
</div>
</div>
</body></code>
<code><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"> <body style="margin-left: 10px;"> <div class="form-check" style="padding-top: 0.5rem;"> <label class="form-check-label"> <input class="toggle form-check-input" type="checkbox" name="newinput"> Open </label> </div> <div id="input_selection"> <div style="height: 0px;" class="collapse-wrapper"> <div class="collapse-content row" style="background-color: red;"> <button type="button" class="add btn btn-primary">Add</button> <p>Some text</p> </div> </div> </div> </body></code>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">

<body style="margin-left: 10px;">
  <div class="form-check" style="padding-top: 0.5rem;">
    <label class="form-check-label">
      <input class="toggle form-check-input" type="checkbox" name="newinput">
      Open
    </label>
  </div>
  <div id="input_selection">
    <div style="height: 0px;" class="collapse-wrapper">
      <div class="collapse-content row" style="background-color: red;">
        <button type="button" class="add btn btn-primary">Add</button>
        <p>Some text</p>
      </div>
    </div>
  </div>
</body>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật