Ensure to preselect the radio button based on the Day's value

I have set up my Radio buttons with corresponding content to be displayed when clicked. I want to automatically pre-select the tab button based on the current day.

For example, if today is Sunday, the page should load showing the contents for Sunday. If it's Tuesday, then the contents of Tuesday should be displayed upon page load.

Below is the code snippet:

@import url('https://fonts.googleapis.com/cssfamily=Open+Sans:400,600,700');
* {
  margin: 0;
  padding: 0;
}

body {
  padding: 50px;
  background: #E5E4E2;
}

.tabinator {
  background: #fff;
  padding: 15px;
  font-family: Open Sans;
}

.tabinator input {
  display: none;
}

.tabinator label {
  box-sizing: border-box;
  display: inline-block;
  padding: 15px 25px;
  color: #ccc;
  margin-bottom: -1px;
  margin-left: -1px;
}

.tabinator label:before {
  content: '';
  display: block;
  width: 100%;
  height: 15px;
  background-color: #fff;
  position: absolute;
  bottom: -11px;
  left: 0;
  z-index: 10;
}

.tabinator label:hover {
  color: #888;
  cursor: pointer;
}

.tabinator input:checked+label {
  position: relative;
  color: #000;
  background: #fff;
  border: 1px solid #bbb;
  border-bottom: 1px solid #fff;
  border-radius: 5px 5px 0 0;
}

.tabinator input:checked+label:after {
  display: block;
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  box-shadow: 0 0 15px #939393;
}

#content1,
#content2,
#content3,
#content4,
#content5,
#content6,
#content7 {
  display: none;
  border-top: 1px solid #bbb;
  padding: 15px;
}

#tab1:checked~#content1,
#tab2:checked~#content2,
#tab3:checked~#content3,
#tab4:checked~#content4,
#tab5:checked~#content5,
#tab6:checked~#content6,
#tab7:checked~#content7 {
  display: block;
  box-shadow: 0 0 15px #939393;
}
<div class="tabinator">

  <input type="radio" id="tab1" name="tabs" checked>
  <label for="tab1">Sunday</label>
  <input type="radio" id="tab2" name="tabs">
  <label for="tab2">Monday</label>
  <input type="radio" id="tab3" name="tabs">
  <label for="tab3">Tuesday</label>
  <input type="radio" id="tab4" name="tabs">
  <label for="tab4">Wednesday</label>
  <input type="radio" id="tab5" name="tabs">
  <label for="tab5">Thursday</label>
  <input type="radio" id="tab6" name="tabs">
  <label for="tab6">Friday</label>
  <input type="radio" id="tab7" name="tabs">
  <label for="tab7">Saturday</label>

  <div id="content1">
    <p>
      This is Sunday
    </p>
  </div>
  <div id="content2">
    <p>This is Monday
    </p>
  </div>
  <div id="content3">
    <p>This is Tuesday
    </p>
  </div>
  <div id="content4">
    <p>This is Wednesday
    </p>
  </div>
  <div id="content5">
    <p>This is Thursday
    </p>
  </div>
  <div id="content6">
    <p>
      This is Friday
    </p>
  </div>
  <div id="content7">
    <p>
      This is Saturday
    </p>
  </div>
</div>

Answer №1

Here is a handy trick you can use:

  1. Retrieve the current day of the week - this corresponds to the index of the radio buttons list, allowing you to easily select the correct tab:

    $('.tabinator input[name=tabs]').eq(new Date().getDay()).prop('checked', true);
    
  2. Fixed some incorrectly labeled labels for the radio buttons.

If you want to display tomorrow's tab instead, you can use the following code snippet:

$('.tabinator input[name=tabs]').eq(new Date().getDay() + 1).prop('checked', true);

Check out the demo below:

$(document).ready(function(){
  $('.tabinator input[name=tabs]') // get all tabs
    .eq(new Date().getDay()) // select the current tab
    .prop('checked', true); // check it
});
@import url('https://fonts.googleapis.com/cssfamily=Open+Sans:400,600,700');
* {
  margin: 0;
  padding: 0;
}

body {
  padding: 50px;
  background: #E5E4E2;
}

.tabinator {
  background: #fff;
  padding: 15px;
  font-family: Open Sans;
}

.tabinator input {
  display: none;
}

.tabinator label {
  box-sizing: border-box;
  display: inline-block;
  padding: 15px 25px;
  color: #ccc;
  margin-bottom: -1px;
  margin-left: -1px;
}

.tabinator label:before {
  content: '';
  display: block;
  width: 100%;
  height: 15px;
  background-color: #fff;
  position: absolute;
  bottom: -11px;
  left: 0;
  z-index: 10;
}

.tabinator label:hover {
  color: #888;
  cursor: pointer;
}

.tabinator input:checked+label {
  position: relative;
  color: #000;
  background: #fff;
  border: 1px solid #bbb;
  border-bottom: 1px solid #fff;
  border-radius: 5px 5px 0 0;
}

.tabinator input:checked+label:after {
  display: block;
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  box-shadow: 0 0 15px #939393;
}

#content1,
#content2,
#content3,
#content4,
#content5,
#content6,
#content7 {
  display: none;
  border-top: 1px solid #bbb;
  padding: 15px;
}

#tab1:checked~#content1,
#tab2:checked~#content2,
#tab3:checked~#content3,
#tab4:checked~#content4 #tab5:checked~#content5,
#tab6:checked~#content6,
#tab7:checked~#content7 {
  display: block;
  box-shadow: 0 0 15px #939393;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="tabinator">

  <input type="radio" id="tab1" name="tabs" checked>
  <label for="tab1">Sunday</label>
  <input type="radio" id="tab2" name="tabs">
  <label for="tab2">Monday</label>
  <input type="radio" id="tab3" name="tabs">
  <label for="tab3">Tuesday</label>
  <input type="radio" id="tab4" name="tabs">
  <label for="tab4">Wednesday</label>
  <input type="radio" id="tab5" name="tabs">
  <label for="tab5">Thursday</label>
  <input type="radio" id="tab6" name="tabs">
  <label for="tab6">Friday</label>
  <input type="radio" id="tab7" name="tabs">
  <label for="tab7">Saturday</label>

  <div id="content1">
    <p>
      This is Sunday
    </p>
  </div>
  <div id="content2">
    <p>This is Monday
    </p>
  </div>
  <div id="content3">
    <p>This is Tuesday
    </p>
  </div>
  <div id="content4">
    <p>This is Wednesday
    </p>
  </div>
  <div id="content5">
    <p>This is Thursday
    </p>
  </div>
  <div id="content6">
    <p>
      This is Friday
    </p>
  </div>
  <div id="content7">
    <p>
      This is Saturday
    </p>
  </div>
</div>

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

Manipulating the DOM in AngularJS Directives without relying on jQuery's help

Let's dive right in. Imagine this as my specific instruction: appDirectives.directive('myDirective', function () { return{ restrict: 'A', templateUrl: 'directives/template.html', link: functio ...

Using Node.js to implement pagination with the POKEapi through a loop

Recently, I've been experimenting with the POKEapi to display information on 150 different pokemons. This is my first attempt at creating a node.js app as I'm just starting to learn javascript. The challenge I'm facing now is implementing ...

What could be causing the submit button to reactivate before all form fields have been completed?

I have implemented the code snippet below to validate each field in my contact form using bootstrap-validator and an additional check through Google reCAPTCHA. You can view and test the form here. The submit button is initially disabled with the following ...

What are the steps to implement localStorage in Vuejs3?

I'm feeling a bit lost when it comes to localStorage and its usage. I have a component called Statistic.vue which displays a modal at the end. Statistic.vue <template> <p class='modal'>{{numberOfGames}}</p> </template& ...

Sending a string data from client to a MongoDB database using Express.js and Node.js with the help of Sails framework

Need help with sending a string from client to mongoDB using Sails The "val" variable represents a string sent in real time from the client to the server database POST Form( is using HTML the best approach here? ) <!DOCTYPE html> <html> < ...

What is the syntax for using escape characters in Vue?

Looking to create a website similar to CodePen? I have developed a library of assets including buttons, cards, and effects, all built using vue.js. Each asset includes HTML, CSS, and sometimes JS code. In my vanilla JS version, I formatted the code using e ...

The offcanvas menu in the NextJS Tailwind website does not handle hover or tap events properly when outside of the parent dimensions

My header includes an off-canvas menu that slides in from the right side. Everything seems to be working fine, except for one issue: when the menu is open and visible, the mouse and touch events pass through it to the content underneath. Check out the cod ...

The challenge of transferring documents to the server

There is a form on the webpage where users can select a file and click a button to send it. <form enctype="multipart/form-data"> <input type="file" id="fileInput" /> <button type="button&quo ...

The use of callbacks in Model.findById() is now deprecated due to a MongooseError

Hello there! I've run into an issue and could use some assistance, thank you in advance! Running MONGOOSE VERSION: "mongoose": "^7.0.3" Error Message: MongooseError: Model.findById() no longer accepts a callback at Function.findB ...

Executing multiple jQuery Ajax requests with promises

I've been learning how to use promises gradually, and now I'm faced with the challenge of handling multiple promises. In my code snippet, I have two email inputs in a form that both create promises. These promises need to be processed before the ...

Creating a user-friendly form in a Nuxt/Vue application that enables dynamic attribute creation

I am currently working on designing a form that enables users to create different variations for a product within a Nuxt/Vue application. The goal is to provide users with the ability to specify attributes for each variation using a text field. These attr ...

The craft of masonry is known for its creation of intentional gaps

I've gone through all the posts related to this issue, but none of the suggested solutions seem to work for me. Here is a visual representation of my grid: https://i.sstatic.net/ybufk.png If you want to see the code and play around with it, check o ...

An AJAX script for dynamically adding, modifying, and removing records from a database

How can I implement a functionality where by pressing "-" the vote is removed from the database, and when any other number is selected, the vote will be updated in the database with that value? The default value of the dropdown list is votacion.votCalific ...

Tips for displaying an array and iterating through its children in Angular 7

I am currently working on extracting the parent and its children to an array in order to display them using ngFor. However, I am encountering an issue where the children are not being displayed during the ngFor. I have a service that retrieves data from a ...

Tips on showcasing numerous texts in a lightbox display

I have created this HTML code snippet <ul class="all-products"> <?php while (have_posts()) :the_post(); ?> <li class="single-product"> <div class="outer-block"> <div class="inner-block"> <div c ...

Ensure users follow step-by-step journey in Vue Material's steppers and connect each step to a designated path

I'm just starting out with web development and I have a question. I want to design a stepper with 5 tabs that restricts navigation - for example, you can't proceed to step 3 without completing step 2, and so on. If a user is on step 4 but wants t ...

Learn how to automatically retrieve messages without refreshing the page by leveraging the power of AJAX

displaying notifications with:- $call = "SELECT * FROM `chat` WHERE fromthe = '$email' and tothe='theadmin' UNION ALL SELECT * FROM `chat` WHERE fromthe = 'theadmin' and tothe='$email' order by id desc"; mysqli_quer ...

I am encountering an issue with retrieving API JSON data in NextJS where I am receiving an

Instead of receiving data in my console log, I am seeing undefined. This is my Index.js file (located in the pages folder) import Head from "next/head"; import Link from "next/link"; import axios from "axios"; import Test fro ...

How to automatically select the first item in a populated dropdown list using Vue JS

My HTML select element is populated with options from a server, but when using v-model, it initially selects an empty option instead of the first one. I came across a solution on a post which suggests selecting the first option manually, but since the dat ...

Show an error notification if the mobile device does not meet the requirements for jquery mobile version

Currently, I am in the process of creating a website using HTML5, CSS3, and jquerymobile. My goal is to have an error message appear saying "Your mobile browser does not support this application" if the page is not rendering correctly or if the jquery mobi ...