Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9253969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:21:02+00:00 2026-06-18T11:21:02+00:00

Bit of a newb to stackoverflow and spending way too much time thinking about

  • 0

Bit of a newb to stackoverflow and spending way too much time thinking about this, but in a nutshell I’m trying to figure out the best approach and least amount of code for this to happen. I’ve got a conditional that will perform an action based on the time of day. See below:

var date = new Date();
var time = date.getHours();

if(time < 12){
header.prepend('Morning ');
link.attr('href', 'css/morning.css');
} else if (time >= 12 && time <= 18){
header.text('its afternoon time');
link.attr('href', 'css/day.css');
} else {
header.prepend('Night ');
link.attr('href', 'css/night.css');
}

I’ve also got a similar action happening with a click event:

$('button').click(function(){
var styleswitcher = $(this).text().toLowerCase();
link.attr('href', 'css/' + styleswitcher + '.css');
});

Is there a way to combine them into one function, I feel like I’m repeating myself by calling the stylesheet twice in the conditional and on the click event. So something along the lines of if the time and the click event match show the proper stylesheet. Thanks for all the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:21:03+00:00Added an answer on June 18, 2026 at 11:21 am

    Yes, just package up the code and put it into the function.

    $(document).ready(function() {
    
        function change_css() {    
            var date = new Date();
            var time = date.getHours();
            var styleSwitcher = 'night';
    
            if(time < 12) {
                styleSwitcher = 'morning';
            }
    
            else if(time >= 12 && time <= 18) {
                styleSwitcher = 'day';
            }
    
            link.attr('href', 'css/' + styleSwitcher + '.css');
        }
    
        change_css(); // First call on initial run.
    
        $('button').click(function() {
            change_css();
        });
    
    });
    

    Note: I haven’t had time to test this code, but the idea is correct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Bit of an odd question but is there a way of seeing what objects
I'm a little bit of a newb when it comes to PHP, but know
Bit flags are a little difficult to understand :) I know about this and
bit of a rough time getting this to work and its an understanding piece
I'm a bit of a newb with mod_rewrite. I'm trying to adjust my dynamic
I've gotten help from others on this, but their replies were a little bit
I'm pretty sure I need a pivot to do this but can't quite figure
I think this is a really newb question, but I never found out the
Bit of a newb but just having a small problem using jquery cycle all
So, i'm a bit of a perl newb. Although I had something much more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.