Press the slide button to reveal hidden text using HTML and CSS

I'm having an issue with my website where I want a button to slide down and reveal text, but currently it's only showing the button with the text already displayed below it. The button isn't functioning as intended when clicked. My goal is for the text to only appear at the bottom once the button is clicked. I tried using .slideDown() but it seems to only make the button visible without any functionality.

    <html>
    <head>
        <style>
            body {
                background: rgb(230, 230, 255);
                color: rgb(0, 0, 0);
                padding: 10px;
                font-family: Georgia;
            }
            header {
                font-size: 1.5em;
                font-weight: bold;
            }
            [id=all-contents] {
                max-width: 800px;
                margin: auto;
            }

            /* navigation menu */
            nav
             {
                background: rgb(239, 80, 41);
                ...
        </style>
        <title>Website</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        
        <script>
            $(document).ready(function(){
                $("button").click(function(){
                    $("ul.professional interests").slideDown();
                });
            });
        </script>
    </head>
    
    <body>
        <div id="all-contents">
            <nav>
              ...
                                     
                  </section>
              </div>
          </main>
      </div>
  </body>
</html>

Answer №1

Here are some corrections and a solution for what I believe you are attempting to accomplish:

  • Avoid using [class=interests] in CSS, instead use the dot notation: .interests!
  • Similarly, refrain from using [id=all-contents] in CSS; it's better to use the pound sign notation: #all-contents !
  • Do not include spaces in class names! Convert professional interests into professional_interests.
  • Your HTML was missing a closing </div>.
  • To target the ul immediately following your button, utilize the .next('ul') method in the script.

Snippet

// Updated script
$(".professional-content button").click(function() {
  $(this).next('ul').slideDown();
});
body {
  background: rgb(230, 230, 255);
  color: rgb(0, 0, 0);
  padding: 10px;
  font-family: Georgia;
}

header {
  font-size: 1.5em;
  font-weight: bold;
}

#all-contents {
  max-width: 800px;
  margin: auto;
}

/* navigation menu styles */

nav {
  background: rgb(239, 80, 41);
  margin: 0 auto;
  margin-bottom: 20px;
  display: flex;
  padding: 10px;
  border-radius: 2em;
}
/* Additional specific styling properties here... */


/* main container beneath menu styles */

main {
  background: rgb(245, 238, 219);
  display: flex;
}
/* More specific styling properties for sidebar, content, etc. */


/* TAKIT: New additions below */

button {
  display: block; /* To enforce a block layout */
}

.professional-content ul {
  display: none;
  transition: 500ms all ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="all-contents">

<!-- Your existing HTML structure with corrected classes and IDs -->

</div>

Answer №2

It can be more convenient if you hide the text you want to display when clicking the button initially.

<div style="display: none;">Hidden Text Here</div>

Then, reveal the text by utilizing this function upon button click:

 function displayText() {
 getElementById("textId").style.display="block";

Lastly, remember to link the function to your button like so:

<button onclick="displayText()">Click here to Show Text</button>

I trust that this will come in handy for you.

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

jQuery selector unable to locate the specified class in the table row

After receiving an array of strings as a response from an SQL query, I am using JavaScript to dynamically add this data as rows to an HTML table: loadUsers: function() { . . . function displayUsersOnTable(response) { for(var i = ...

Tips on getting the Jquery .load() function to trigger just once and executing an Ajax request only once

Upon loading the page, I am utilizing the JQuery .load() function to retrieve content from a PHP file. The content loads successfully but it keeps reloading continuously as observed through Chrome Developer tools. I only want the content to load once. var ...

Expanding the width of a single svg element within a block of three

Is there a way to stretch one of the three SVGs in a block without any space between them? The first and last SVG should always be 100px width, while the second SVG should have varying widths like 100px, 200px, or 1000px but must be adjacent to each other. ...

Are you looking to enhance your website with dynamic and

I am looking to display dynamic text in a label using HTML. This label should be populated with text from a Javascript function upon loading or reloading the page. How can I make this happen? <label id="MyLabel"></label> <script type="tex ...

In Javascript, initiate a global variable using the window[] syntax

Currently, I am making an ajax request and handling the success callback: var specific_id = $(this).attr('id'); $.ajax({ type: "GET", url: "/tw/www/search/", data: {search:tw_search, type:'tw ...

Using importXML with Internet Explorer 11

My project website includes a roster page that retrieves XML data. Previously, this functionality worked across all browsers but now it only works in Chrome. In IE11, it seems that the importXML function is not functioning correctly as the roster data is m ...

Bootstrap grid not maintaining consistent column widths

Currently, I am in the process of setting up a responsive bootstrap grid for the projects section on my portfolio page. My goal is to create a 3x3 grid that adjusts seamlessly for mobile devices and breaks down into a 2-column grid and eventually a 1x6 gri ...

How can I utilize the HTML returned by an XHR call to reload the entire page?

When an XHR call retrieves HTML content, how can I instruct the browser to load that content and refresh the entire page? Typically, XHR is used for partial page updates, but my situation requires a different approach. Thank you. In broader terms, what is ...

Align the content inside a ul element using Bootstrap 4's justify feature

Hello, I am currently using Bootstrap 4 and have a question regarding the "justify-content-around" property. When there are more than three li elements, it works perfectly, but if there are less than three, it does not work. Any tips on how to fix this iss ...

Manipulating CSS Class Properties Using JavaScript

In my JavaScript application, there is a functionality that loads a list of items for users to click and view detailed information in a separate div on the page. Users should be able to interact with and make modifications to these individual item overview ...

The document type is not compatible with the element "BR" in this location; it is assumed that the "LI" start-tag is missing

How can I create a validated list with HTML 4.01 standards? I tried the following code, but it's giving me an error: "document type does not allow element 'BR' here; assuming missing 'LI' start-tag" <tr> <td colspan="2" ...

What is the method for retrieving the name of a canceled file?

When a user clicks on the "Cancel" button during an upload, I am attempting to retrieve the filename of the file that was canceled. However, it seems like the current method I am using (var image_file_name) is not successfully retrieving the name of the ca ...

How can we create a reusable dropdown list that appears when clicking on different elements?

Imagine having different sections of a webpage that, when clicked, reveal a dropdown list right below the selected link. I've noticed some websites accomplish this by actually having just one dropdown list defined in the DOM, which then appears below ...

Utilize React to float the Material UI SwipeableDrawer component to the right side of the screen

I'm currently working on a project that involves using material UI alongside React/Redux. There has been a recent change in the Figma file where the SwipeableDrawer component is now positioned on the right side of the screen instead of the left. I&apo ...

I need to locate a div element with the class name of "dhx_cal_light" and adjust its height style to 460px

Is there a way to locate a div element with the class "dhx_cal_light" and modify its height to 460px? Here is the HTML snippet: <div class="dhx_cal_light" style="visibility: visible; height: 511px; display: block; top: 80px; left: 633px;"> This is ...

What is the reason behind hyperlinks breaking the continuity of the text on a line? Is there a

When I have a line of code containing several hyperlinks, my desired output is a single line. However, despite using CSS to set white-space to nowrap, the result is not as expected. The line of code in question is: <a href="profile.php?v=<?php echo ...

What is the best way to ensure that third tier menus open in line with their respective items?

Check out this fiddle link: http://jsfiddle.net/Wss5A/72/ Focusing on the following: 2-A-1 2-A-2 2-A-3 The third-tier menu is currently displayed starting from the top left corner. I want the first submenu element to be aligned with either 2-A-1, 2-A- ...

I have noticed that the require.js define object is mysteriously appearing in my code within gulp.js

I have a gulp-concat script set up to concatenate jquery, slick-carousel, t.js, and my index.js files. const pack = () => { return gulp.src(['./node_modules/jquery/**/jquery.js', './node_modules/slick-carousel/**/slick.js', &apos ...

Adding to the beginning of a list in JQuery mobile

Having trouble figuring out how to prepend my list in jQuery mobile while keeping the divider on top of the most recent item added. I attempted to prepend the newly added item, but it ended up shifting the divider to the bottom instead. function loadScan ...

Create a new tab without triggering the pop-up blocker by utilizing an iframe's JavaScript event

I currently have an iframe embedded in a webpage. Once data is successfully sent to the server within the iframe, the server responds with a new URL to be opened either in a new tab or the parent window. The main issue I am encountering is that the brows ...