When I toggle the div to close on mobile, I want it to show on desktop

My attempt at creating a toggle button to hide and show content was not successful. I struggle with coding in Javascript/Jquery.

In the desktop view, the description displays perfectly (see image here), but in mobile view, there is a button to toggle hiding and showing the description (image here).

However, when I hide the description in mobile view, it disappears in the desktop view if I resize the browser window. Only when I show the description in mobile does it reappear on desktop. How can I fix this issue?

Javascript:

//Script
<script>
    $(document).ready(function () {
        $(".toogle-button").click(function () {
            $(".toggleholder").slideToggle("slow");
            $(this).find("i").toggleClass("fa-chevron-down");
        });
    });
</script>

Html:

//html
<div class="toogle-button">Introduction<i class="pull-right fa fa-chevron-up fa-chevron-down"></i></div>
<div class="toggleholder">
    <div class="desc-holder">
        <p>
            <p><b>Subject:</b></p>
            <span class="side-text">Design</span>
        </p>
        <p>
            <p><b>Description:</b></p>
            <span class="side-text">This quiz is to test your knowledge on HTML basic foundation.</span>
        </p>
    </div>
    <div class="user-holder">
    <p><b>Assigned By:</b></p>
    <div class="profile-holder">
        <div class="avatar-holder">
            <img src="/include/images/default/avartar_32.png" class="avatar32" />
        </div>
        <div class="username">Admin Asriah Asadi</div>
        <div class="clearfix"></div>
    </div>
</div>

Css/Less:

.toogle-button {
display: none;
margin-top: 15px;
padding: 10px;
background-color: #055eac;
@media only screen and (max-width: @screen-xs-max) {
    display: block;
  }
}

Answer №1

One effective approach is to utilize the window resize event handler as shown below:

$(window).resize(function()
 {
   var screenWidthBreakpoint = 768; //adjust this value based on your requirements
   if ($(window).width() > screenWidthBreakpoint) $('.toggleholder').show(); //display toggleholder for desktop 
   else $('.toggleholder').hide(); //hide toggleholder for mobile devices
});

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

Is there a way to produce a unique color using Stylus CSS?

Currently, I'm utilizing Express for Node.js and Stylus as the CSS engine. While Stylus is great, I'm facing some challenges in passing color variables or generating random colors. Despite attempting to use the javascript API for stylus, I find m ...

What is the process for decoding HTML content that is wrapped within JSON data?

I have a web application built using asp.net that utilizes ajax calls. Below is the code snippet for my web method which successfully responds to the ajax call. ADController adc = new ADController(); DataTable dt = adc.GetGeneral(Convert.ToInt32(A ...

Automatically updating CSS file in progress

Is there a way to have CSS changes automatically update on my template without requiring me to manually refresh the page? I've noticed this question being asked multiple times, but none of the solutions provided seem to work for me. I attempted usin ...

What is the equivalent of preg_replace in PHP using jquery/javascript replace?

One useful feature of the preg_replace function in PHP is its ability to limit the number of replacements made. This means that you can specify certain occurrences to be replaced while leaving others untouched. For example, you could replace the first occu ...

The alignment of Material-UI Button and ButtonGroup is not consistent

I'm puzzled as to why the Button and ButtonGroup elements are not aligned on the baseline in the code snippet provided. Is there a specific property that can be adjusted on the ButtonGroup element to achieve alignment? <!DOCTYPE html> <htm ...

Using Node.js to access the public stream of app.net for streaming purposes

Exploring the world of streaming and HTTP long living connections for the first time. This is where I'm at: const accessToken = require('./config.js').accessToken; const https = require('https'); const req = https.request({ ...

Displaying numerous database rows through SQL with the aid of PHP and JavaScript (specifically AJAX) on the frontend of a website

I am a beginner in the world of PHP and JavaScript (Ajax) and despite my efforts to find a solution by looking at various posts and websites, I have not been successful. My goal is to display all related database rows on the front end of a website. While ...

Strange occurrence with z-index, list items that are floating are not being displayed on top of the heading

I have come across a unique problem with z-indexes that I have never experienced before. To address one common speculation right away: the positioning for each element with a z-index is already set properly, so that's not the issue. Despite my efforts ...

Building a dynamic and fast Vite project using "lit-ts" to create a visually appealing static website

I recently put together a project using Vite Lit Element Typescript and everything seemed to be running smoothly on the development server. However, when I tried running npm run build, only the compiled JS file was outputted to the /dist folder without any ...

Activate jquery code during interaction

I am in need of a scalable image that can be centered within a scalable <div>. Currently, I have a wrapper <div> along with the scalable <div> that contains the image. Additionally, there is some jQuery code in place to calculate the widt ...

Looking for a JavaScript (Angular) event listener to trigger when closing pages and tabs

I am looking for an event that will only work when closing a page or tab, but should not be triggered when the page is refreshed. I am aware of the "beforeunload" event, but it also gets activated on page refresh. Below is the code snippet I am currently ...

Whenever I nest my div tags within another div element in an HTML document

Whenever I try to position a div inside another div using float, I encounter problems. <div id="main"> <div id="anotherDiv"> <h1>Test</h1> </div> </div> Here is the corresponding style sheet: #main{ ba ...

Enhance the speed of webview on Android devices

I have been working on an application that utilizes a webview to load both HTML and JavaScript files. Unfortunately, I have noticed a significant decrease in performance when displaying the content. Despite trying various solutions such as: webVi ...

Tips for rearranging array position retrieved from API in Vue.js

props: { groups: Array, }, computed: { groups: function(arr) { alert('hi') } }, <div v-for="(item, index) in groups" :key="item.id" :id="item.id" class="group-name" @click="isOnlySelected ? null : $emit('setSele ...

Using jQuery to trigger alert only once variable has been updated

I have a question that may seem too basic, but I can't find the solution. How do I make sure that the variables are updated before triggering the alert? I've heard about using callbacks, but in this case, there are two functions and I'm not ...

How can I integrate the native calendar of an Android device in Phonegap?

What is the best way to add an event to an Android device's calendar using phonegap? I need to make sure it works on Android Version 2.3 and above. The available plugins are not functioning correctly, so I am looking for alternative solutions. ...

Transferring a variable value between functions using autocomplete and AJAX communication

I am facing difficulties with implementing autocomplete jQuery along with AJAX call. The issue arises when a user enters text in the input field, triggering an AJAX POST request to the controller which retrieves values from the database and sends them back ...

Guide on loading numerous JavaScript files into a database using the mongo shell

I am faced with the task of loading multiple js files that contain collections creations and seeds into my database using the mongo command. Currently, I have been manually loading data from each file one by one like so: mongo <host>:<port>/< ...

Python and JavaScript fundamental interaction

My current setup involves having a local HTML page named leaflet.html, which is being shown in an embedded browser within a python-tkinter application. Within the leaflet.html file, there exists a straightforward JavaScript code snippet that includes a fu ...

Animating multiple elements in Angular 2 using a single function

Currently, I am delving into Angular and faced a challenge while attempting to create a toggle categories menu. Within my navbar component, I have an animation trigger set up as follows: trigger('slideCategory', [ state('opened&apo ...