Javascript accordion functionality breaks when attempting to add a plus sign

As a beginner in development, I am working on implementing an accordion in JavaScript. In my HTML file, I have separate divs for question titles and another div for the "+" sign. My objective is to change the "+" sign to "-" and reveal the answer when a user clicks on a div with the class "accordionTitle". Can someone provide guidance on the best approach to achieve this?

HTML:

<section class="qnaSection">
    <h1 class="qnaTitle">FAQ</h1>
    <div id="qnaContainer" class="qnaContainer"></div>
</section>

CSS:

.qnaSection {
  background-color: rgb(35, 57, 94);
  color: var(--brown);
}

.qnaTitle {
  text-align: center;
  margin: 10px 0;
  font-size: 28px;
}

.qnaContainer {
  width: 90%;
  max-width: 1000px;
  margin: 20px auto;
}

.accordionItem {
  background-color: rgb(222, 222, 222);
  color: black;
  margin: 10px 0;
  border-radius: 10px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.25);
  
}
/* Rest of the CSS remains unchanged */

toggleBar()

Answer №1

If you're looking to improve your code, one approach is to avoid embedding HTML within JavaScript. Instead, consider dynamically generating elements using

document.createElement("elementName")
, then assigning a class name or ID to the element. You can also add an onclick event listener to the element and update its content by modifying innerText or innerHTML with a simple "-" character. The resources available at this link may provide additional guidance. Feel free to reach out if you have any further inquiries!

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

Rendering components asynchronously in ReactJS

After completing my ajax request, I need to render my component. Here is a snippet of the code: var CategoriesSetup = React.createClass({ render: function(){ var rows = []; $.get('http://foobar.io/api/v1/listings/categories/&apo ...

Steps for eliminating duplicates in an array while considering additional keys

Consider the following arrays: const a1=[ { a:{ name: "first" } }, { b:{ name: "second" } }, { c:{ name: "third" } }, { d:{ name: "fourth" } } ] const a2=[ { a:{ name: "first", shelf: "read" } }, { b:{ name: ...

What is the purpose of the tabindex in the MUI Modal component?

Struggling with integrating a modal into my project - it's refusing to close and taking up the entire screen height. On inspection, I found this troublesome code: [tabindex]: outline: none; height: 100% How can I remove height: 100% from the ...

Failure to launch mail client from mailto attribute

Hey there! I'm currently diving into an HTML course on udemy, and there's this interesting part where the instructor showcases how a "mailto" attribute can be used to open a mail client upon clicking the submit button. I've followed all the ...

What is the best way to create a screen capture of a webpage using a URL?

Currently working on a Spring MVC website project, I have implemented a form requesting the user's website URL. Once entered, I aim to display a screenshot of the specified website for the user to view. Contemplating whether to generate the image on ...

The hiding/showing of elements is not being executed correctly by jQuery

My web template includes HTML and jQuery code for a project I'm working on. During the $.getJSON call, there can be a delay in loading the data. To inform the user to wait, I added a warning message in a div with the id="warning". The code properly ...

When tapping on the Facebook feed share button on an iOS 5 device, the keyboard fails to disappear

I am developing the app using HTML5 and JS. Within the app, there is a feature that allows users to connect with their Facebook account. However, when they try to post something on their Facebook wall and tap the Share button, the SoftKeyboard on iOS does ...

Storing information in a database with multiple entries

Displaying a list of inactive users from a mysqli database in a table format, each user has a row with three drop-down menu options - admin, delete user, and activate user. The table is populated using a prepared statement to fetch data from the database. ...

When attempting to insert data retrieved from axios into a table within a React component, the data is coming back as

Hi there! Currently, I'm in the process of developing an application that retrieves data from an API and my goal is to display it in a Material UI Table within a React environment. Strange issue I'm encountering: when I use console.log to check ...

Assigning null values, or initializing values in AngularJS

How can I clear input values to null after clicking on a button? Empty fields: https://i.sstatic.net/JDTZA.jpg Fields with existing values that I want to reset back to empty fields. https://i.sstatic.net/T5iKc.jpg HTML: <label class="item item ...

Find a partial match in the regular expression

I am currently working on filtering a list of names using regular expressions (regex). The data is stored in the following format: [ { "firstName": "Jhon", "lastName": "Doe", }, ... ] Users have the option t ...

Save the environment value within Vue when the app starts up

When working with a basic web app created using VueJS, the application makes an API call that returns an object containing the environment name. For instance: https://appsdev/mysimpleapp/api/environment returns {"applicationName":"My S ...

Combine the elements within the HEAD tag into a group

I am currently developing a dynamic page system using AJAX. Whenever a link is clicked, an AJAX call is made to retrieve data from the requested page. However, I have encountered an issue where certain data in the page file needs to be placed within the h ...

The white background of the div conceals the shadow of the Bootstrap navbar

I am using Bootstrap's navbar with .shadow-sm. I have two divs beneath it, one without background and the other one with a white background. The shadow appears as expected in the div with no background specified, but it is being obscured by the white ...

What is the best way to enlarge the text in an image input field?

One of the input fields I'm working with looks like this: <input type="image" name="submit" value="submit" src="images/searchb1.png" id="button1"/> I am trying to figure out how to enlarge the text Submit on that field using CSS. Any suggestio ...

Using jQuery to dynamically assign default selection in HTML

Currently, I have a line of code that dynamically sets the default selection of a selection box in html using jQuery. var y=...a value from the DB; this.$(".status option[value=y]").attr("selected", "selected"); The issue is that it only works if I ...

Looking to retrieve country, state, city, and area based on inputting a pincode value using Node.js?

I'm currently working on a web project with nodeJs and ejs. I am looking for a solution that can automatically update the country, state, city, and area fields based on the input of a pin-code (zip-code). Are there any recommended packages in node js ...

Background image color not displaying correctly

I've been attempting to overlay a transparent color on top of a background image. While the background image is displaying correctly, I'm having trouble getting the color to show up. I noticed that this question has been asked before. I tried im ...

Struggling to connect to an express route?

I'm currently working on a MERN project and I'm encountering a small issue. When trying to make a POST request to a specific route, Express throws a 404 error and tells me that it can't find the desired route. However, everything works perfe ...

A Bootstrap carousel displaying images in a confused manner with another carousel on the same webpage

I recently encountered an issue with adding two Bootstrap 5 carousels on the same page. Even after assigning unique IDs to each carousel, the second carousel seems to mix photos with the first one, duplicating two photos from the previous slide. I tried ...