Tips for toggling the visibility of a revolution slider based on current time using JavaScript

Currently, I am integrating the revolution slider into my WordPress website. My goal is to have the slider change according to the standard time of day. For instance, I want slider1 to display in the morning, slider2 at noon, slider3 in the evening, and slider4 at midnight.

To achieve this, I attempted to use JavaScript by assigning unique class names to each slider element. Below is an excerpt of the code:

<script>
var time = new Date().getHours(); 
if (time >= 8) {
document.getElementsByClassName('morningslide').style.visibility='visible';
document.getElementsByClassName('noonslide').style.visibility='hidden';
document.getElementsByClassName('eveningslide').style.visibility='hidden';
document.getElementsByClassName('midnightslide').style.visibility='hidden';
}


if (time >= 12) {
document.getElementsByClassName('appBanner').style.visibility = 'hidden';
document.getElementsByClassName('noonslide').style.visibility='visible';
document.getElementsByClassName('eveningslide').style.visibility='hidden';
document.getElementsByClassName('midnightslide').style.visibility='hidden';
}


if (time >= 18) {
document.getElementsByClassName('morningslide').style.visibility='hidden';
document.getElementsByClassName('noonslide').style.visibility='hidden';
document.getElementsByClassName('eveningslide').style.visibility='visible';
document.getElementsByClassName('midnightslide').style.visibility='hidden';
}


if (time >= 22) {
document.getElementsByClassName('morningslide').style.visibility='hidden';
document.getElementsByClassName('noonslide').style.visibility='hidden';
document.getElementsByClassName('eveningslide').style.visibility='hidden';
document.getElementsByClassName('midnightslide').style.visibility='visible';
}
</script>

Answer №1

When using document.getElementsByClassName()
, it is important to note that it returns a collection of elements rather than individual elements themselves. In order to style the elements, you will need to loop through the collection and set the style of each one individually if they are not unique. Alternatively, if they are unique, you can simply select the first element from the collection using [0]. However, I would recommend giving each element a unique ID and using document.getElementById() to directly target the specific element you want to style.

Answer №2

After implementing your code on a test html page, I can confirm that it is functional. The only adjustment I made was utilizing IDs instead of classes and 'display' in place of 'visibility'. Both options work effectively. Assuming you intend to place this in the footer of the webpage, here is the revised code.

<!doctype html>
<html>
<head>

</head>

<body>

<div>
    <div id="morningslide">Hello this is Morning</div>
    <div id="noonslide">Hello this is Noon</div>
    <div id="eveningslide">Hello this is Evening</div>
    <div id="midnightslide">Hello this is Midnight</div>
</div>
</body>

<script>
    var time = new Date().getHours();
    document.getElementById('morningslide').style.display='none';
    document.getElementById('noonslide').style.display='none';
    document.getElementById('eveningslide').style.display='none';
    document.getElementById('midnightslide').style.display='none';

    if (time > 8 && time < 11) {
        document.getElementById('morningslide').style.display='block';
    }
    else if (time >= 12 && time <= 18) {
        document.getElementById('noonslide').style.display='block';
    }
    else if (time > 18 && time <= 22) {
        document.getElementById('eveningslide').style.display='block';
    }
    else if (time > 22) {
        document.getElementById('midnightslide').style.display='block';
    }

</script>
</html>

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

Unable to retrieve input using jquery selector

I am attempting to detect the click event on a checkbox. The Xpath for the element provided by firebug is as follows, starting with a table tag in my JSP (i.e. the table is within a div). /html/body/div[1]/div/div/div[2]/div/div[2]/div[1]/div/div[2]/table ...

Using ASP .NET controls in conjunction with Bootstrap

Is there a way to easily apply bootstrap classes to all asp .net controls in my application at once? Here is an example of how I formatted my menu control using bootstrap. I am looking for a solution where I can apply the bootstrap theme commonly to all m ...

What prevents me from extending an Express Request Type?

My current code looks like this: import { Request, Response, NextFunction } from 'express'; interface IUserRequest extends Request { user: User; } async use(req: IUserRequest, res: Response, next: NextFunction) { const apiKey: string = ...

How can I programmatically close the Date/Time Picker in Material UI Library?

Is there a way to programmatically close the Date/Time Picker in Material UI Library? ...

Issue with Vue-Validator form validation not functioning properly on JS Fiddle

I'm having trouble with vue-validator on JSFiddle. Can someone please assist in troubleshooting the issue so I can proceed with my main question? Check out JSFiddle Html: <div id="app"> <validator name="instanceForm"> & ...

Creating redux reducers that rely on the state of other reducers

Working on a dynamic React/Redux application where users can add and interact with "widgets" in a 2D space, allowing for multiple selections at once. The current state tree outline is as follows... { widgets: { widget_1: { x: 100, y: 200 }, widg ...

`I'm experiencing difficulty sending JSON data to Typeahead using PHP`

I am having trouble passing an array of data from PHP to typeahead. I have tried various solutions but nothing seems to work. When I display the response on the console, it shows the array of strings but they are not populating in the typeahead field. PHP ...

Guide on setting the href value within a $.each loop in AJAX requests

I am struggling with downloading a file within a thread. The issue I'm facing is that I can't set the file name in the href and download attributes, so when I attempt to download it, it shows no file available. However, I did realize that replaci ...

I am encountering an issue where my like button is returning a Json object { liked: true } but it is not functioning correctly with my ajax call in my Django application whenever a user tries to click the

Having trouble creating a like button with an AJAX function in Django. Every time I click on the button, it redirects me to a different page showing a JSON object that returns {liked: true}. Why is this happening? It's been quite challenging for me to ...

Using Ajax and jQuery to dynamically insert an option into a datalist

I'm in the process of building a search box using Flask, MySQL, and ajax. I've managed to retrieve the search results in JSON format within the console, but now I want to dynamically add them to the options in my datalist element in the HTML. He ...

Combine theme configuration options within Material-UI

I am interested in setting up custom theme rules in Material-UI. My goal is to create both light and dark themes and extend them with some shared settings. My initial idea was to store the common settings for the light and dark themes in a separate variab ...

Using backslashes to escape JSON values within a value in Angular

When retrieving JSON data from the backend, I often encounter an issue where the value is set to "key": "\$hello" and it results in an "Unexpected token d". Is there a way in Angular to handle or escape these characters once received from the server? ...

jquery module for retrieving and updating messages

I want to develop a custom plugin that can be utilized in a manner similar to the following example. This isn't exactly how I plan to use it, but it serves as the initial test for me to fully grasp its functionality. HTML: <div id="myDiv">< ...

Unable to choose a child div using jQuery

<div class='class1'> <div class='class2'> <div class='class3'> some unique text </div> <div class='class5'> more unique text </div> </div> < ...

What steps do I need to follow in order to incorporate and utilize an npm package within my Astro component

I have been working on integrating KeenSlider into my project by installing it from npm. However, I am encountering an error message that says Uncaught ReferenceError: KeenSlider is not defined whenever I try to use the package in my Astro component. Belo ...

Comparing the Calculation of CSS Selector Specificity: Class versus Elements [archived]

Closed. This question requires additional information for debugging purposes. It is not currently accepting answers. ...

Disconnect occurred during execution of Node.js "hello world" on a Windows 7 operating system

Issue Resolved: Oh no! jimw gets 10000 points for the solution! I've decided to delve into a hobby project using Node.js. Here's where I started: Downloaded and installed Node version 0.6.14 Copied and pasted the classic "hello world" program ...

Styling Text with Shadow Effects in Mobile Web Browsers

I'm attempting to apply a text-stroke effect using text-shadow like this: .text-stroke { text-shadow: -1px -1px 0 white, 1px -1px 0 white, -1px 1px 0 white, 1px 1px 0 white; } Unfortunately, it's not rendering correctly on mobile versions of Fi ...

Using CSS3 to Enhance the Look of Multiple Background Images

While I've come across similar inquiries, I haven't found a satisfactory explanation yet. My aim is to grasp the best CSS practices for implementing multiple repeating backgrounds. Essentially, I want a background image to repeat across the entir ...

Swap out the image backdrop by utilizing the forward and backward buttons

I am currently working on developing a Character Selection feature for Airconsole. I had the idea of implementing this using a Jquery method similar to a Gallery. In order to achieve this, I require a previous button, a next button, and the character disp ...