If the width of the window is greater than 1080, do this action. Otherwise, switch back to using CSS and ensure to check for changes upon

I am creating a section on a webpage where the height is determined by screen.availHeight. It works perfectly, but I also want it to reset to the default height in the CSS when the width goes below 1080px. I've tried different approaches, but haven't been successful. The current if statement only uses screen.availHeight if the width is above 1080px, but doesn't reset when the width decreases again.

Additionally, I want it to check the width both on page load and when resized using (window).resize. Is there a way to combine both (window).resize and (document).ready to check the width on initial load and upon resizing?

<script>
$(window).on('load resize',function(){ //Updated line that now functions correctly
    if ($(window).width() > 1080)
    {
        var screenHeight = screen.availHeight;
        $('#home').css('height', screenHeight + 'px');
    }
    else
    {
        $('home').css('height', '');
    }
});
</script>

Here is a demonstration of the webpage -

If you try loading the page with a browser width under 1080px, everything is fine. Then resize above 1080px, still good. But when you reduce the width again, you'll encounter one issue. Refreshing the page at a width above 1080px will reveal another problem. This is my first project involving JavaScript, so I might be overlooking something simple.

The first problem has been resolved - does anyone have a solution for reverting to CSS in the else statement?

Answer №1

If you want to bind a function to multiple events, you can use the .on() method like this:

$(document).on('click mouseover keydown',function() {

    ...

});

Alternatively, you can directly pass the function as a parameter to individual event functions like so:

var myFunction = function() {

   ...

}

$(document)
    .click(myFunction)
    .mouseover(myFunction)
    .keydown(myFunction)

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

Steps to Retrieve and Showcase a Compilation of YouTube Clips with JavaScript

Recently, I managed to write a C code that retrieves the list of YouTube videos from the URL "*http://gdata.youtube.com/feeds/api/standardfeeds/top_rated*" using the libsoup library. By utilizing libxml2, I was able to parse the XML data and extract specif ...

Creating your own browser extension similar to Firebug: A comprehensive guide

As a beginner dotnet developer, I am looking to create an Internet Explorer addon similar to Firebug in Firefox. Specifically, I need the HTML and CSS features of Firebug without all the extras. I'm not sure where to start or which platform to choose. ...

Personalize the style of ordered lists

Greetings! I am attempting to customize the style of an ordered list like the one shown in the image on another website. https://i.sstatic.net/BQok4.png However, instead of the circle used in that picture, I want to use a different CSS Image like the one ...

Is Apache/PHP adding additional content to web pages? Causing website malfunctions?

I am experiencing some difficulties with my website after setting up a new machine on RHEL5. I suspect the issue might be related to the httpd configuration, but I haven't been able to pinpoint it yet. To troubleshoot, I ran a simple Jquery .get() co ...

Can someone help me transform this LESS code into SCSS?

Currently, I am working on adapting a theme from [www.bootswatch.com][1] that is originally in less format to scss for my rails application. Although I am not well-versed in either SCSS or LESS, I have been researching the variances and have identified so ...

Alerts during script execution

As I execute the script, I encounter the following warnings: Notice: Undefined variable: varName in C:\wamp\www\dash\index.php on line 38 Notice: Undefined variable: varMsg in C:\wamp\www\dash\index.php on line 38 N ...

Oops! Looks like there was an error: weight is not defined when trying to calculate BMI with the HTMLButtonElement

I'm currently working on a project to create a BMI calculator following specific instructions. I've managed to follow all the guidelines except one regarding the letsCalculateBMI function. The instruction states: letsCalculateBMI should extrac ...

What is causing the scripts to fail when I attempt to press a button?

Clicking on the button is supposed to slowly reveal phone numbers on the page. Here are the HTML Codes: <span id="show-phone-numbers" class="btn btn-success"> <i class="fe fe-phone-call"></i> Show Phone Nu ...

Ways to reset input fields following form submission

I've been trying to figure out how to clear the input fields once the form is submitted, but for some reason, the input data remains there even after each submission. I'm using ajax jquery form. Any ideas on how to resolve this issue? Thank you ...

Utilizing Multiple Checkboxes for Precision Search Refinement

Big thanks to Khalid Ali for the support provided up until now. I am currently working with an array of songs that each have descriptions, keywords, etc. I have a set of checkboxes that I want to use to refine a search. Essentially, if someone selects the ...

Trouble with displaying vertical scroll bar in React app when height is set to 100%

I am developing a React application where I need the content to always occupy at least 100% of the height to style the background accordingly. I have attempted to achieve this by setting the CSS height to 100% at the top level, which works fine. However, t ...

"JQuery Mobile: Successful AJAX requests when working offline, but encountering issues when online

I am experiencing a peculiar issue where my code works perfectly on my local computer, but when I try to access a remote server using AJAX, it always fails. The code snippet is as follows: $.ajax({ url: web_url + 'getSightseeing.php ...

Linking a watcher property to control the opacity value of inline styles

Struggling to connect the opacity of a div with the value of a slider. <div class="container" v-bind:style="opacity">test content</div> Despite my efforts, I can't seem to make the binding work correctly. When I check in the developer to ...

Using PHP to output HTML tags as text

How can I display an HTML tag as text on screen, rather than in code format? I want readers to be able to easily read the tag itself. What is considered the best practice for achieving this? print htmlspecialchars('<meta name="copyright" conten ...

Looking for an Angular Material component that displays a list of attributes?

I am trying to create a dynamic list of properties in Angular using Material Design that adjusts for different screen sizes. For example, something similar to this design: https://i.stack.imgur.com/EUsmV.png If the screen size decreases, the labels shou ...

Jquery executes b.fn.b.init[102] and displays the output

My task involves selecting multiple elements, all of which have the class "org-box". Within each box, there is a link that I need to capture the href from. Here is my code: $("a.org-box").click(function (e) { e.preventDefault(); var link = $(this).attr( ...

Create a dynamic MUI icon that adapts to different screen sizes

Struggling to make the MUI DeleteIcon responsive and clickable. <ListItem> <ListItemAvatar> <Avatar src={sprites.front_default}> <ImageIcon /> </Avatar> </ListItemAvatar> <ListItemText pr ...

Discovering the worth of a key within a JSON file

Below is a JSON with objects containing artist and image values. I need a function that, given an artist name, will return the corresponding image value in the same object. All objects are enclosed in an array as a JSON. var iTunes_data = $([{ "titl ...

Refresh required to showcase newly created items

Currently, I am developing a straightforward tool that allows users to assign budgets to teams. This tool operates in a manner similar to a standard to-do list. The delete function functions smoothly without requiring a reload. However, the create functio ...

Ensuring proper alignment of images and text in HTML when resizing

Trying to show an image and text side by side, I'm using the following code: div.out { background-color: silver; border: 1px solid black; padding: 10px; display: flex; } div.right { order: 1; background-color: white; border: 1px soli ...