How can JavaScript be used to run a script only when the screen width is less than 480, and not when it is greater than 480?

Hi there, I have a simple script that I need help with. Despite not being well-versed in JavaScript, I am using Bourbon and Neat for the frontend of my website. On one particular page, I want to implement a collapse script from Bourbon refills. My question is: how can I modify this script to only activate if the screen width is less than 480px? If the screen width is greater than or equal to 480px, I do not want the script to run.

Here is the HTML code:

<div class="expander">
  <a href="javascript:void(0)" class="expander-trigger expander-hidden">Expandable section</a>
  <div class="expander-content">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Distinctio mollitia fugiat facilis enim accusamus quisquam aut, repellendus incidunt quod optio facere labore illo numquam ipsum beatae vero debitis, fugit excepturi.</p>
  </div>
</div>

And here is the JavaScript code:

$(document).ready(function() {
  $('.expander-trigger').click(function(){
    $(this).toggleClass("expander-hidden");
  });
});

Answer №1

$(document).ready(function() {
  $('.click-trigger').on('click', function(){
    if($(window).width() < 480) { // checking the window width
      $(this).toggleClass("hidden-expander");
    }
  });
});

UPDATE: After discussing in the comments, this is the updated solution

$(document).on('resize', function() {
    if($(window).width() < 480) { // checking window width
        // window width is less than 480 pixels
    } else {
        // window width is more than 480 pixels
    }
});

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 transfer variables to a different page through a pop-up window?

I'm working with a function that converts the Id of the clicked element into a variable, then opens a new window with a different page. How can I access or utilize this variable on the newly opened page? var idToWrite = []; $(function(){ $(".szl ...

Searching for the value of a data attribute using an AJAX GET request

I am looking to apply styles based on the data attribute value matching an AJAX get request Here is my jQuery code: jQuery(function($) { get_stock(); function get_stock(){ $.ajax({ url: vdisain_vd.url, ...

Utilize Fb.api to automatically post on user's wall upon logging in

I want to utilize fb.api to make a single post on the logged-in user's wall. Here is the code snippet: var params = {}; params['message'] = 'gegeegeggegall! Check out www.facebook.com/trashcandyrock for more info.'; params['n ...

What is the best way to add a style to the currently active link on a NavLink component using the mui styled() function

I have a custom NavLink component that I want to style with an ".active" class when it is active. However, I am not sure how to achieve this using the "styled()" function in MUI. Does anyone know how to accomplish this? Below is the code for my custom Nav ...

Can a callout or indicator be created when a table breaks across columns or pages?

As we develop HTML pages for printing purposes, one specific requirement for tables is to include an indicator like "Continues..." below the table whenever a page or column break occurs. Additionally, in the header of the continuation of the table, we need ...

Enhance ngx-bootstrap's tab content with ngx-scrollbar functionality

Currently in my project, I am utilizing bootstrap 4 and ngx-bootstrap. One requirement I have is to develop a component consisting of 2 scrollable divs that can be switched by tabs. I was hoping to demonstrate a sample application on stackblitz, but unfor ...

Choosing from a list in Angular

I'm trying to make a dropdown menu that shows options in the format "code-description", but only displays the "code" portion when an option is selected. For example, showing "A-Apple" in the dropdown, but displaying only "A" when chosen. I am able to ...

What is the best way to place a single button above a row using the bootstrap btn-group?

I currently have 11 buttons arranged in 3 rows, and I am trying to position one button at the top as a standalone element before the two rows of buttons. However, despite experimenting with various flex-end and content align classes, I have not been succ ...

Animate my banner images only after they have fully loaded using Jquery

I recently came across a banner image slideshow in jQuery, but it seems to be animating even before the images are fully loaded. This results in the slideshow displaying image descriptions without the actual images themselves. Can anyone help me modify th ...

Facing a problem with displaying Leadbolt HTML ads?

I'm currently working on incorporating leadbolt HTML ads into my Android app, which utilizes a WebView to display HTML for the user interface. I have been provided with this simple tag from my leadbolt control panel: <script type="text/javascript ...

Exploring jQuery AJAX and how to effectively manage various data types

ASP.Net MVC is the framework I am currently using, but this issue can apply to any framework out there. When making an Ajax call to my server, most of the time it returns plain HTML content. However, in case of an error, I want it to return a JSON object ...

Running a JavaScript function directly in the browser's address bar with GWT

Attempting to run Javascript in my web application by entering the following in the browser URL/address bar: javascript:window.alert('test');void(0); Despite this, the alert box fails to display. Is it possible that the application is in DevMod ...

Making AngularJS 'PUT' requests: The process of submitting only the data in a form

I am facing an issue while updating user data in Angular. When I send a 'PUT' request, the entire user $scope is being sent instead of only the fields visible on the form. To retrieve and update the data, I am using a Factory. Below is my edit f ...

ng filtering with a controller-defined scope

I am currently working on a webpage with AngularJS and I am looking to implement some filters on the site. Here is the HTML code I have: <div ng-repeat="data in datas | filter:{area:course} | filter:{subject:subFilter} | filter:{city:cityFilter}"> ...

Troubleshooting JavaScript issues within pop-up windows using Chrome Debugger

Is there a more efficient method for debugging a popup window in Chrome? I want to debug the code when the window is opened and I'm looking for a process similar to using Visual Studio where I can set a breakpoint on JS and have the debugger stop at t ...

Having trouble with @babel/plugin-proposal-optional-chaining in Vue.js <script> tag

While working on my vue/vuetify project and attempting to implement optional chaining, I consistently run into an error: ./src/App.vue?vue&type=script&lang=ts& (./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??v ...

"The data provided was invalid" - Building a Razor Pages powered .NET Core Web API

Requesting assistance from anyone who can help! I am encountering an issue with the post method in Postman where I am only receiving the message "The input was not valid." Can someone please assist me with this? I have already spent a significant amount of ...

The floating label appears distorted when used with a datalist input in Bootstrap 5

How can I get the floating label to display correctly for datalists in Bootstrap 5? Currently, it looks like this... It's working fine without the form-floating class, but I want to use the floating form feature. <div class="form-floating" ...

Guide on utilizing jQuery to create a popup window for streaming Vimeo or YouTube videos

Currently working on the landing page design for a new iPhone app and thinking of incorporating a video modal window similar to the one on: . Came across this helpful thread on "Popup Jquery window to play youtube" but unsure how to resize the video to pr ...

"My PHP headers are not working properly when I try to

When I invoke a script with JavaScript var user = document.getElementById('username').value; var pass = document.getElementById('password').value; var conf = document.getElementById('confirm').value; var code ...