How can I create a DIV element that consistently shows text when clicked?

Is there a way to achieve this effect using just CSS and HTML? When clicking on the question text, it should reveal the answer below, and clicking again should hide the answer.

I have managed to make it work with hover, but I am unsure how to implement it for click interactions. Here is the current HTML code:

<div id="packagequestioninfo">
  <p class="packagereplies">Question</p>
  <p class="packagecomment">If you require <b>hosting</b> for your website, select your primary website and go to <b>Part II</b>. If you do not require hosting, please Checkout below. </p>
</div>

CSS:

#packagequestioninfo {
padding: 10px;
background: #F2F7FA;
border-radius: 0px;
-moz-box-shadow: 0 0 5px #ccc;
-webkit-box-shadow: 0 0 5px #ccc;
box-shadow: 0 0 5px #ccc;
}

#packagequestioninfo:hover {
background-image:url(../img/index/body/ourproducts/light_blue_background_pattern.jpg);
background-repeat:repeat;
cursor: none;
}

#packagequestioninfo .packagecomment {
display: none;

}

#packagequestioninfo:hover .packagereplies {
display: none;
}   
#packagequestioninfo:hover .packagecomment {
display: inline;
}

You can view the code on this JSFiddle link.

Answer №1

Hide the .packagecomment element using CSS and then create a jQuery function:

$('button').click(function(){
     $('div').toggle();
});

Check out the demo: http://fiddle.jshell.net/ABC123/

Answer №2

To achieve this without requiring support for IE8 and earlier versions, you can utilize pure CSS as demonstrated in this example. This approach leverages the ":checked" css selector. For more information on browser support, refer to this resource.

Here is the HTML structure:

<div class="question">
    <input type="checkbox" class="qestion-checkbox" id="q1" />
    <label for="q1" class="qestion-text">Question 1 text</label>
    <label for="q1" class="qestion-answer">Question 1 answer</label>
</div>
<div class="question">
    <input type="checkbox" class="qestion-checkbox" id="q2" />
    <label for="q2" class="qestion-text">Question 2 text</label>
    <label for="q2" class="qestion-answer">Question 2 answer</label>
</div>

Below is the associated CSS code:

.qestion-answer, .qestion-checkbox {
    display: none;
}
.qestion-checkbox:checked + .qestion-text {
    display:none;
}
.qestion-checkbox:checked + .qestion-text + .qestion-answer {
    display:block;
}

If you require support for IE8 and older browsers, you will need to incorporate some javascript/jQuery into your solution.

Answer №3

Using only CSS, achieving this is not possible. You could potentially switch from using :hover to :active, but that would require holding down the mouse button for it to work. Take a look at an example.

Have you considered using jQuery instead? It might make things easier.

Answer №4

Here is a non-jQuery way to show and hide content by changing the display style attribute:

HTML:

<script src="javascriptfile.js"></script>
<div id="packagequestioninfo">
  <p class="packagereplies">Question</p>
  <p class="packagecomment">If you require <b>hosting</b> for your website, select your primary website and go to <b>Part II</b>. If you do not require hosting, please Checkout below. </p>
</div>

Javascriptfile.js:

// Wait for all window elements to load
window.onload = function() {
  document.addEventListener('packagequestioninfo').addEventListener('onclick', function() {

    if(document.getElementById('packagecomment').style.display !== 'none') {

      document.getElementById('packagecomment').style.display = 'none';

    } else {

      document.getElementById('packagecomment').style.display = 'block';

    }

  });
}

If you prefer the class toggling method:

window.onload = function() {
  document.addEventListener('packagequestioninfo').addEventListener('onclick', function()     {

    if(document.getElementById('packagecomment').className.indexOf('show')) {

      document.getElementById('packagecomment').className = '';

    } else {

      document.getElementById('packagecomment').className = 'show';

    }
  });
}

In this approach, make sure to add a 'show' class to your CSS:

.show{
  display:block;
}

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

Discover the job specifications pages on Dice.Com by utilizing C programming language

I have taken on a student project involving web scraping job postings from Dice.Com for analysis. My main focus is on extracting the job description, but I am struggling to figure out how to access it. With limited knowledge in HTML and C#, I find it dif ...

Transform text that represents a numerical value in any base into an actual number

Looking to convert a base36 string back to a double value. The original double is 0.3128540377812142. When converting it to base 36: (0.3128540377812142).toString(36); The results are : Chrome: 0.b9ginb6s73gd1bfel7npv0wwmi Firefox: 0.b9ginb6s73e Now, h ...

The previous link is still present in the current URL

Having some trouble with my button code here. I'm new to HTML and I have a button that references another HTML file using a parameter. <a class="btn btn-primary" role="button" href="reto_resultado/{{ clave_primaria }}">Check</a> The issu ...

Tips for positioning a div at the bottom using Bootstrap

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d5f5252494e494f5c4d7d08130c130e">[email protected]</a>/dist/css/bootstrap.min.css"> <div class="co ...

Unable to Retrieve HTML Element by TagName using VB

I am currently working on a project in VB that involves retrieving elements by tag names, similar to what I used to do in VBA. However, I am facing challenges as my code seems to freeze after opening a new browser window. Public ie As New SHDocVw.Intern ...

Managing selected ticket IDs in a table with AngularJS

I have a table that includes options for navigating to the next and previous pages using corresponding buttons. When I trigger actions for moving to the previous or next page (via controller methods), I store the IDs of checked tickets in an array $scope. ...

Tips for customizing the appearance of path elements within an external SVG file being utilized as a background image

How can I change the color of the paths in an SVG background image assigned to a div using CSS? ...

Can floating elements be disregarded by block elements?

According to W3C, the behavior of floating elements is such that: When a float is present, non-positioned block boxes that come before and after the float flow vertically as if the float doesn't exist. However, line boxes positioned next to the fl ...

The active tabs or placeholders do not update properly when I click on them

Is there anyone who can help figure out why the tabs are not functioning as expected? The goal is for the tabs to change the input field placeholder and perform a search based on the active tab. If you're able to assist, please review my complete cod ...

Jquery Menu featuring subitems aligned to the right

I am experiencing an issue with my drop-down menu in ie6. The subitems menus are shifted to the right, rendering the menu useless. Here is the HTML code: <div id="navigation"> <a href="<?php echo base_url();?>" class="singl ...

How can I alter the background color while performing a transformation in JS/CSS?

I'm currently working on achieving a flipboard effect for some divs, where one side is white and the other is black. Here's what I have so far: setInterval(function () { document.getElementById('test').classList.toggle('flipped& ...

How to Utilize USB Devices with Node-Webkit?

I am in the process of creating a node-webkit application that must be compatible with all three major desktop operating systems (Windows, Mac, and Linux). My goal is to establish a connection between my app and a USB device that is plugged in, but I am en ...

Choose and manipulate a set of elements using Jquery

Below is a piece of code I have: <div class="timeline"> <div class="timeslice" data-tid="360"></div> <div class="timeslice" data-tid="360"></div> <div class="timeslice" data-tid="360"></div> <di ...

What could be the reason for the malfunction of the min value in my Material UI date textfield?

I am attempting to set a minimum date for the picker to start from, but it does not seem to be working as expected. <TextField id="date" type="date" defaultValue="2017-05-24" minDate="24/01/2019" ...

Display the form-control field only when a selection has been made in the previous form-control using HTML and PHP

I'm in the process of developing a webpage with form controls (2 in this example). Here's my code: <body> <div class="option_choice_global"> Select your options <div class="col-xs-2"> <lab ...

Mobile Size Setting

Could someone please explain to me why when I test this code on Google Chrome using the mobile emulator for Galaxy S3, it displays the correct size (640x360), but when I try to load it on my actual Galaxy S5 or any other device, the size is different from ...

Is it possible to link an HTML select element to a changing array in JavaScript?

Let's say I have an empty HTML select element. <select id="options"> </select> Can I link a JavaScript array to this select so that when the array is modified, the select options update accordingly? Alternatively, do I need to resort to ...

Using JQuery to Load Text Content from Textarea into an IFrame

Seeking a solution to transfer HTML textarea content into an IFrame upon button click. Any suggestions on achieving this functionality? Thank you in advance. Illustratively, when a user types Hello World in the textarea and clicks the submit button, the t ...

How to keep jQuery horizontal submenu open when clicking on the menu

Seeking assistance with a menu issue. I have a horizontal menu with subitems that display when clicked, but having trouble with the submenu behavior. When clicking on a submenu item, I want it to remain open and show the related items underneath it. For ex ...

What could be the reason for the lack of response from the jquery element on an

Recently, I've been working on a jQuery project with sticky headers. However, when I tested it on my iPad, I noticed a slight delay and the header jumping around the page. I'm wondering if this issue can be resolved within the code itself or if ...