1

I want to hide and show overflow content in a div.If i click on expand bautton it expand. But i want this button should be toggle and text also be change. Thnks

   function descriptionHeightMore(){
          var myElement = document.querySelector(".backwhite");
        myElement.style.height = "auto";
        }
 .backwhite{
        background-color: aqua;
        padding:15px;
        height:50px;
        overflow:hidden;
      }
   <div class="container">
     <div class="backwhite">
     <p>1. Create Ui For Email Campaign.</p>
     <p>2. Create Functionality of Email Campaign</p>
     <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
     <p>1. Create Ui For Email Campaign.</p>
     <p>2. Create Functionality of Email Campaign</p>
     <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
     <p>1. Create Ui For Email Campaign.</p>
     <p>2. Create Functionality of Email Campaign</p>
     <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
    </div>
    <button type="button" class="btn btn-default btn-block"     onclick="descriptionHeightMore();">Expand</button>
    </div>

8 Answers 8

1

Here's a solution with plain JavaScript

First, you should create a new class called .expanded which we can toggle on and off -

.backwhite.expanded {
    height: auto;
}

Next, I modified the JavaScript to toggle the .expanded class, as well as the content of the button -

function toggleDescriptionHeight(e) {

  // Toggle .expanded class
  document.querySelector(".backwhite").classList.toggle('expanded');

  // Toggle textContent of the button
  e.target.textContent == 'Expand' ? e.target.textContent = 'Collapse' : e.target.textContent = 'Expand'; 

}

// Add event listener to button click   
var button = document.querySelector('.btn');
button.addEventListener('click', toggleDescriptionHeight)

Link to a working example on CodePen - http://codepen.io/ire/pen/wgyvYw

Sign up to request clarification or add additional context in comments.

4 Comments

Hello Ire Aderinokun, Your code is working well if i run it in codepen but if i run it in normal html page then it is not collapse or expand. Can you check it.and thnks a lot for help me.
Hi @AjayKumar , can you send me a link to the page? I've done it locally and it works
Thanks Dear for your support, i did some mistake while writing code, now your code run very well.Again Thanks a lot !!
Awesome! Glad to help :)
1

with javascript Try this, first check the height and then decide:

    function descriptionHeightMore(){
    	 var myElement = document.querySelector(".backwhite");
        if(myElement.style.height == "auto"){
         myElement.style.height = "50px";
        }else{
            myElement.style.height = "auto";
        }
    }
  .backwhite{
    background-color: aqua;
    padding:15px;
    height:50px;
    overflow:hidden;
  }
<div class="container">
 <div class="backwhite">
 <p>1. Create Ui For Email Campaign.</p>
 <p>2. Create Functionality of Email Campaign</p>
 <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
 <p>1. Create Ui For Email Campaign.</p>
 <p>2. Create Functionality of Email Campaign</p>
 <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
 <p>1. Create Ui For Email Campaign.</p>
 <p>2. Create Functionality of Email Campaign</p>
 <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
</div>
<button type="button" class="btn btn-default btn-block"  onclick="descriptionHeightMore();">Toggle</button>
</div>

Comments

1

To get element's height by javascript is impossible .You have button for changing height so checking on its button text is easy to make toggle changing .

function descriptionHeightMore(t){
          var myElement = document.querySelector(".backwhite");       
          if(t.innerHTML == "Expand") {
            myElement.style.height = "auto";
            t.innerHTML = "Collapse";
            }
          else {
            myElement.style.height = "50px";
            t.innerHTML = "Expand";
            }
        }
.backwhite{
        background-color: aqua;
        padding:15px;
        height:50px;
        overflow:hidden;
      }
<div class="container">
     <div class="backwhite">
     <p>1. Create Ui For Email Campaign.</p>
     <p>2. Create Functionality of Email Campaign</p>
     <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
     <p>1. Create Ui For Email Campaign.</p>
     <p>2. Create Functionality of Email Campaign</p>
     <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
     <p>1. Create Ui For Email Campaign.</p>
     <p>2. Create Functionality of Email Campaign</p>
     <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
    </div>
    <button type="button" class="btn btn-default btn-block"     onclick="descriptionHeightMore(this);">Expand</button>
    </div>

Comments

0

Since you have labelled question in jQuery, You can achieve this easily with toggleClass function of jQuery

Working Example https://jsfiddle.net/yyx93shy/

<script>
   function descriptionHeightMore(){
      var myElement = document.querySelector(".backwhite");
      $(myElement).toggleClass("autoHeight");
   }
</script>

CSS

.autoHeight {
   height: auto;
}

Comments

0

$(".btn").click(function() {

  $(".backwhite").toggleClass("overflow")
  $(".backwhite").hasClass("overflow") ? $(".btn").text("Expand") : $(".btn").text("Compress")

})
.backwhite {
  background-color: aqua;
  padding: 15px;
  height: 50px;
}
.overflow {
  overflow: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="backwhite overflow">
    <p>1. Create Ui For Email Campaign.</p>
    <p>2. Create Functionality of Email Campaign</p>
    <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
    <p>1. Create Ui For Email Campaign.</p>
    <p>2. Create Functionality of Email Campaign</p>
    <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
    <p>1. Create Ui For Email Campaign.</p>
    <p>2. Create Functionality of Email Campaign</p>
    <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
  </div>
  <button type="button" class="btn btn-default btn-block">Expand</button>
</div>

  1. use .toggleClass()
  2. have a predefined class for overflow.
  3. toggle accordingly

Comments

0

Check it out guys

<!DOCTYPE html>
<html>
<head>
<style type="text/css">

   .backwhite{
    background-color: aqua;
    padding:15px;
    height:50px;
    overflow:hidden;
  } 

</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1  /jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
     var myElement = document.querySelector(".backwhite");

     if(myElement.style.height == "auto"){
     myElement.style.height = "50px";
    }else{
        myElement.style.height = "auto";
    }
    });
});

</script>
</head>
<body>

 <div class="backwhite">
 <p>1. Create Ui For Email Campaign.</p>
 <p>2. Create Functionality of Email Campaign</p>
 <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
 <p>1. Create Ui For Email Campaign.</p>
 <p>2. Create Functionality of Email Campaign</p>
 <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
 <p>1. Create Ui For Email Campaign.</p>
 <p>2. Create Functionality of Email Campaign</p>
 <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
</div>

<button type="button">Toggle</button>

</body>
</html>

2 Comments

Thnks Aravind Bhat K Can you help me to change text with toggle.
change text in the sense,you no longer need hide and show in that?
0

Try this code. It will change the overflow and the button value on a click of a button. good luck

<html>

  <head>
  </head>

  <body>
    <script>
      function descriptionHeightMore() {
        var myElement = document.querySelector(".backwhite");
        myElement.style.height = "auto";
        if (myElement.style.overflow == "hidden")
          myElement.style.overflow = "visible";
        else
          myElement.style.overflow = "hidden";
        var myButton = document.getElementsByTagName("button");
        if (myButton.value == "expand")
          myButton.value = "collapse";
        else
          myButton.value = "expand";
      }
    </script>

    <style>
      .backwhite {
        background-color: aqua;
        padding: 15px;
        height: 50px;
        overflow: hidden;
      }
    </style>

    <div class="container">
    <div class="backwhite">
      <p>1. Create Ui For Email Campaign.</p>
      <p>2. Create Functionality of Email Campaign</p>
      <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
      <p>1. Create Ui For Email Campaign.</p>
      <p>2. Create Functionality of Email Campaign</p>
      <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
    </div>
    <button type="button" class="btn btn-default btn-block" onclick="descriptionHeightMore();">Expand</button>
  </div>
</body>

</html>

function descriptionHeightMore() {
  var myElement = document.querySelector(".backwhite");
  myElement.style.height = "auto";
  if (myElement.style.overflow == "hidden")
    myElement.style.overflow = "visible";
  else
    myElement.style.overflow = "hidden";
  var myButton = document.getElementsByTagName("button");
  if (myButton.value == "expand")
    myButton.value = "collapse";
  else
    myButton.value = "expand";
}
.backwhite {
  background-color: aqua;
  padding: 15px;
  height: 50px;
  overflow: hidden;
}
<div class="container">
  <div class="backwhite">
    <p>1. Create Ui For Email Campaign.</p>
    <p>2. Create Functionality of Email Campaign</p>
    <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
    <p>1. Create Ui For Email Campaign.</p>
    <p>2. Create Functionality of Email Campaign</p>
    <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
  </div>
  <button type="button" class="btn btn-default btn-block" onclick="descriptionHeightMore();">Expand</button>
</div>

Comments

-1

Use jquery method to toggle Ex:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
    $("p").toggle();
  });
});
</script>
</head>
<body>

<p>This is a paragraph.</p>

<button>Toggle between hide() and show()</button>

</body>
</html>

4 Comments

copy paste from W3school
doesn't matter..i hope you also use w3schools.@kritikaTalwar
convert your answer to the OP requirement to show your point.
posted check it out@Suchit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.