JavaScript JS, Use HTML instead of variable

You can simply change the display CSS-property of the element in question

var status = "less";

function toggleText()
{
    
    if (status == "less") {
        document.getElementById("textArea").style.display = "block";
        document.getElementById("toggleButton").innerText = "See Less";
        status = "more";
    } else if (status == "more") {
        document.getElementById("textArea").style.display = "none";
        document.getElementById("toggleButton").innerText = "See More";
        status = "less"
    }
}
<div id="textArea" style="display: none;">
    Here I want to implement HTML code and show it when clicked "See More"
</div>


<a id="toggleButton" onclick="toggleText();" target='_blank' href="javascript:void(0);">See More</a>
Answer:1

you can add a class to the container of both elements to change both of their statuses together.

Here, I add the class less-mode to the container to both show the text inside the textArea and change the button to show less instead of show more.

Instead of saving the state in the javascript, we can know the state by checking if the class already exists on the container.

There are two "modes":

  • when container has the class less-mode, make textArea display: block to show it, and show the less element inside the link.
  • when container doesn't have the class less-mode, make textArea display: none to hide it completely, and show the more element inside the link.

This way, most of the responsibility for the visual change lay in the CSS and HTML. the JavaScript code only switches between them

const containerElement = document.getElementById("container");

function toggleText() {

  if (containerElement.classList.contains('less-mode')) {
    containerElement.classList.remove('less-mode');
  } else {
    containerElement.classList.add('less-mode');
  }
}
.more-btn .less {
  display: none;
}

.more-btn .more {
  display: block;
}

#textArea {
  display: none;
}

.container.less-mode #textArea {
  display: block;
}

.container.less-mode .more-btn .less {
  display: block;
}

.container.less-mode .more-btn .more {
  display: none;
}

.red {
  background: red;
  color: white;
  padding: 5px;
}
<div class="container" id="container">
  <a id="toggleButton" class="more-btn" onclick="toggleText();" target='_blank' href="javascript:void(0);">
    <div class="more">See More</div>
    <div class="less">See Less</div>
  </a>
  <div id="textArea">
    <h3>Title</h3>
    <div class="red">red text</div>
  </div>
</div>
Answer:2

str = prompt("enter a string"); rev = str.split("").join("").reverse(); console.log(rev); Guys, I am trying to reverse an array using the above code but I am getting an error stating "Type error ....

str = prompt("enter a string"); rev = str.split("").join("").reverse(); console.log(rev); Guys, I am trying to reverse an array using the above code but I am getting an error stating "Type error ....

This is my code. function m1() { return new Promise(function(resolve, reject) { var files = $('#aws-upload-files')[0].files; if (files.length > 0) { for (var i = 0; i < files....

This is my code. function m1() { return new Promise(function(resolve, reject) { var files = $('#aws-upload-files')[0].files; if (files.length > 0) { for (var i = 0; i < files....

I have this donut chart currently working in an AngularJS app: But the design mockup says we would like this, note the border-radius property on the green portion of the arc: How do I add a border-...

I have this donut chart currently working in an AngularJS app: But the design mockup says we would like this, note the border-radius property on the green portion of the arc: How do I add a border-...

  1. border radius property in css
  2. border radius property in bootstrap 4
  3. border radius property
  4. border-radius property not working
  5. border radius property for all browsers
  6. border radius property in react native
  7. border radius property not working in ie
  8. border-radius property mdn
  9. css radius property
  10. border radius invalid property value
  11. border radius shorthand property
  12. css border radius property for all browsers
  13. border-radius the property is marked invalid
  14. css3 border-radius property

I am using bootstrap datetimepicker have to get the datetime and add days to it var get_date_time = Friday, October 23rd 2015, 12:00:00 am format get_date_time to oct 25 2015 12:00:00 am how to ...

I am using bootstrap datetimepicker have to get the datetime and add days to it var get_date_time = Friday, October 23rd 2015, 12:00:00 am format get_date_time to oct 25 2015 12:00:00 am how to ...