When activating a bootstrap class button, I must ensure the reply-box remains hidden

let replyButton = document.getElementById('reply-button');
let cardBody = document.getElementById('card-body');
let responseBox = document.getElementById('reply-box');
let cancelButton = document.getElementById('btn btn-danger');

replyButton.onclick = function() {
  responseBox.style.display = "contents";
}
cancelButton.onclick = function() {
  cardBody.style.display = "none";
}
<button class="btn" id="reply-button">Reply</button>
    <div id="reply-box" class="card" style="display: none">
        <div class="card-body">
            <textarea class="form-control"></textarea>
            <br>
            <div class="form-group">
                <button class="btn btn-primary">Post Response</button>
                <button class="btn btn-danger">Cancel</button>
            </div>
        </div>
    </div>

How can I hide all response boxes when pressing the cancel bootstrap button?

Answer №1

Your classes/id are incorrect:

  • card-body is supposed to be a class, but you have it listed as an id
  • The id reply-button does not exist in your HTML
  • The id cancel-button is missing from your HTML
  • Using
    document.getElementById('btn btn-danger')
    will not work as it requires an id, not multiple classes.

Here is the corrected code:

let btn = document.getElementById('reply-button');
let crdbody = document.getElementById('card-body');
let replybox = document.getElementById('reply-box');
let cancel = document.getElementById('cancel-button');

btn.onclick = function() {
  replybox.style.display = "contents";
}
cancel.onclick = function() {
  crdbody.style.display = "none";
}
<div id="reply-box" class="card">
  <div id="card-body">
    <textarea class="form-control"></textarea>
    <br>
    <div class="form-group">
      <button class="btn btn-primary" id="reply-button">Post Reply</button>
      <button class="btn btn-danger" id="cancel-button">Cancel</button>
    </div>
  </div>
</div>

It should be noted that display: contents; is still in draft status for CSS (see https://caniuse.com/?search=display%3Acontents and https://drafts.csswg.org/css-display/) which means it may change and isn't a finalized standard yet, so use with caution in production code.

Update if you cannot modify the HTML:

let btn = document.getElementById('reply-button');
let replybox = document.getElementById('reply-box');
let cancel = document.querySelector('.btn.btn-danger');

btn.onclick = function() {
  replybox.style.display = "contents";
}
cancel.onclick = function() {
  replybox.style.display = "none";
}
<button class="btn" id="reply-button">Reply</button>
<div id="reply-box" class="card" style="display: none">
  <div class="card-body">
    <textarea class="form-control"></textarea>
    <br>
    <div class="form-group">
      <button class="btn btn-primary">Post Reply</button>
      <button class="btn btn-danger">Cancel</button> </div>
  </div>
</div>

When selecting elements by classes, you can use document.querySelector (for single elements) and document.querySelectorAll (for multiple elements).

Additionally, I removed the unnecessary crdbody variable and adjusted the script to properly show/hide the replybox multiple times.

Answer №2

Give this a shot. Is this the outcome you're looking for?

let button=document.querySelector('#reply-box .btn.btn-primary');
let replyBox=document.getElementById('reply-box');
let cancelButton=document.querySelector('#reply-box .btn.btn-danger');
button.onclick=function(){
    replyBox.style.display="contents";
}
cancelButton.onclick=function(){
    replyBox.style.display="none";
}

button.click(); // just to test... it will display the reply box initially.
    <div id="reply-box" class="card" style="display: none">
        <div class="card-body">
            <textarea class="form-control"></textarea>
            <br>
            <div class="form-group">
                <button class="btn btn-primary">Post your reply</button>
                <button class="btn btn-danger">Cancel</button>
            </div>
        </div>
    </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to add custom styles to an Ext JS 'tabpanel' xtype using the 'style

Is there a way to change the style of a Ext.tab.Panel element using inline CSS structure like how it's done for a xtype: button element? { xtype: "button", itemId: "imageUploadButton1", text: "Uploader", style: { background : ' ...

What is the best way to ensure the width of the div is adjusted to fit the table it contains?

What is the best way to adjust the width of a div containing a potentially wider-than-screen table? Despite setting padding: 10px for the div, the right padding disappears when the table exceeds the screen width. Below is an example code snippet: <div ...

Deciphering JavaScript's Minus Equals Operator: What's the Significance?

Can you explain the significance of the minus equals symbol -= in the following code snippet? $('#wrapper').animate({ backgroundPosition: '-=2px' })(); Appreciate your help! ...

Reasoning behind splitting this solution into a function utilizing Javascript

Today, while working on a coderbyte problem, I managed to solve it on my own without any outside help, and it felt fantastic. However, upon completion, I realized that I solved it without creating any functions. Rather, I kept referring back to a previous ...

What is the impact of util.inherits on the prototype chain in JavaScript?

Exploring this particular design: Function ConstrA () { EventEmitter.call(this); } util.inherits(ConstrA, EventEmitter); var obj = new ConstrA(); If util.inherits is omitted, ConstrA and obj will each establish their own distinct prototype chain. T ...

Avoid overwriting the success response parameter in jQuery Ajax

I've encountered a perplexing issue where the response parameter from the first ajax call is being overridden by the second call's parameter. Here is the code snippet: http://pastebin.com/degWRs3V Whenever both drawDonutForExternalLogin and dra ...

Node.js module loader compared to client-side AMD loader such as RequireJS

Today, let's talk about loading Javascript modules on the client-side. There are two popular ways to do this: Using RequireJS Utilizing NPM (Node Package Manager) for exporting and requiring files I've always found the first option to work wel ...

Two-column dropdowns without the use of tables

I am looking to create a drop-down menu with the following structure: | Heading ---------------- action | Item action | Item action | Item action | Item The action could represent "Change" and Item could be something like "Users". To maintain ...

Ajax Complete adds Jquery two times in a row

I have a simple ajax complete call that is designed to add some text after an ajax load finishes. However, I'm encountering an issue where the information is sometimes displayed multiple times. I suspect there might be something missing in my approach ...

Using Cordova for mobile applications to play local video files

Our iOS app, developed using PhoneGap / Cordova 4.3.0, loads an external website directly utilizing <content src="http://example.com/foo" /> in the config.xml file. All functionality resides within this website, eliminating the need for local HTML or ...

(Critical) Comparing AJAX GET Requests and HTTP GET Requests: identifying the true client

When a typical GET request is made through the browser, it can be said that the browser acts as the client. However, who exactly serves as the client in the case of a GET request via AJAX? Although it still occurs within the browser, I am intrigued to delv ...

Are you looking for straightforward dynamic directives that come with dynamic controllers and a scope?

Feeling like I have a simple problem to solve here. Working within the confines of a TypeScript + Angular application. Within a controller, I've got an array of similar directives that I want to utilize. These are essentially the panels strewn throug ...

Tips for applying CSS styles exclusively to the homepage

I am currently managing a SMF forum and I am facing an issue while trying to apply a background image exclusively to the homepage. Whenever I add a style to the .body class, it changes the background of not just the homepage but also other sections like ...

What is the method for obtaining the CSS text content from an external stylesheet?

I've spent countless hours trying to achieve the desired results, but unfortunately, I haven't been successful. Below is a summary of my efforts so far. Any helpful tips or suggestions would be greatly appreciated. Thank you in advance. Upon rec ...

The Android Webview is experiencing difficulties with loading CSS styles and displaying the blockquote font color effect

I am facing an issue with loading HTML content in CSS through Android WebView. Despite having the code executed, I don't see any observable changes in font color for the text inside blockquote tags Here is the HTML code: <html> <head> ...

Tips for extracting specific values from JavaScript using jQuery in ASP to be utilized in the C# code behind section

I want to extract the selected values from a multiselect jQuery and use them in the code behind in C#. The script sources can be found at the following link: https://github.com/nobleclem/jQuery-MultiSelect Below is the ASP code I am using: <div class ...

Will my JavaScript function be triggered when the page is resized while printing?

I have a simple HTML layout where I am using the TextFill jQuery library to automatically adjust the text size based on available space. Everything looks good on screen, but when printed, it seems like the page is resized and the JavaScript needs to be re ...

Difficulties in Adjusting the Size of Three Image Columns within a Website's Body

While working on my website layout, I encountered a problem with inserting three image columns in the body section. The images turned out to be too large and extended beyond the footer. Moreover, the third image was larger than expected, dominating the web ...

Exploring the implementation of async.each with request in the Node.js Express framework

I'm currently working on a node.js project where I need to send multiple API requests in an iterative manner, so I decided to use the async.each method for this purpose. However, I encountered an issue where the console reported that the 'url&ap ...

Unexpected error arises in Typescript despite code functioning properly

As part of a practice project where I'm focusing on using webpack, ES6, npm and Typescript implementation, I have successfully set up loaders for 'awesome-typescript-loader' and 'babel-loader', which are bundling the code properly. ...