using JQuery, add a class on click event or on page load

Solved It!

After encountering a problem created by some sloppy moves on my part, I managed to solve it. However, another issue arose: adding the class current to li with data-tab="tab-1 upon page load.

$('ul.tabs li').click(function(){
        var tab_id = $(this).attr('data-tab');

        $('ul.tabs li').removeClass('current');
        $('.tab-content').removeClass('current');

        $(this).addClass('current');
        $("#"+tab_id).addClass('current');
    });
.tabs li.current {
  color: red;
}
.tab-content {
  display: none;
 }
.tab-content.current {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="innovation">
  <div class="categories-wrap">
    <ul class="tabs">
      <li class="tab-link" data-tab="tab-1">
        <i class="sprite-call-black"></i>
        <h4>Pristine coverage</h4>
        <p>Enjoy your calls without interuptions like dropped calls, poor sound quality, and delayed video.</p>
      </li>
      <li class="tab-link" data-tab="tab-2">
        <i class="sprite-call-black"></i>
        <h4>Chat messaging</h4>
        <p>Chat in real time with connections all around the world. </p>
      </li>
      <li class="tab-link" data-tab="tab-3">
        <i class="sprite-call-black"></i>
        <h4>Video calling</h4>
        <p>WiFi paired with reliable cellular service is how we’ve got you covered in more places than ever before.</p>
      </li>
      <li class="tab-link" data-tab="tab-4">
        <i class="sprite-call-black"></i>
        <h4>Photo share</h4>
        <p>WiFi paired with reliable cellular service is how we’ve got you covered in more places than ever before.</p>
      </li>
    </ul>
  </div>
  <div class="tabs-wrap">
    <div id="tab-1" class="tab-content current">
      <img src="" alt="Pristine Coverage">
    </div>
    <div id="tab-2" class="tab-content">
      <img src="" alt="Chat Messaging">
    </div>
    <div id="tab-3" class="tab-content">
      <img src="" alt="Video Calling">
    </div>
    <div id="tab-4" class="tab-content">
      <img src="" alt="Photo Share">
    </div>
  </div>
</section>

Answer №1

Should this be used?

$(document).ready(function () {
  $(".active").removeClass("active");
  $(".tabs-container #tab-1").addClass("active");
});

Answer №2

Introduce a method to automatically switch to the first tab upon page load:

$('ul.tabs li').click(function() {
  var tab_id = $(this).attr('data-tab');

  $('ul.tabs li').removeClass('current');
  $('.tab-content').removeClass('current');

  $(this).addClass('current');
  $("#" + tab_id).addClass('current');
});

//Trigger click on first tab
$("[data-tab='tab-1']").click();
.tabs li.current {
  color: red;
}
.tab-content {
  display: none;
}
.tab-content.current {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="innovation">
  <div class="categories-wrap">
    <ul class="tabs">
      <li class="tab-link" data-tab="tab-1">
        <i class="sprite-call-black"></i>
        <h4>Pristine coverage</h4>
        <p>Enjoy your calls without interruptions like dropped calls, poor sound quality, and delayed video.</p>
      </li>
      <li class="tab-link" data-tab="tab-2">
        <i class="sprite-call-black"></i>
        <h4>Chat messaging</h4>
        <p>Chat in real time with connections all around the world.</p>
      </li>
      <li class="tab-link" data-tab="tab-3">
        <i class="sprite-call-black"></i>
        <h4>Video calling</h4>
        <p>WiFi paired with reliable cellular service is how we’ve got you covered in more places than ever before.</p>
      </li>
      <li class="tab-link" data-tab="tab-4">
        <i class="sprite-call-black"></i>
        <h4>Photo share</h4>
        <p>WiFi paired with reliable cellular service is how we’ve got you covered in more places than ever before.</p>
      </li>
    </ul>
  </div>
  <div class="tabs-wrap">
    <div id="tab-1" class="tab-content current">
      <img src="" alt="Pristine Coverage">
    </div>
    <div id="tab-2" class="tab-content">
      <img src="" alt="Chat Messaging">
    </div>
    <div id="tab-3" class="tab-content">
      <img src="" alt="Video Calling">
    </div>
    <div id="tab-4" class="tab-content">
      <img src="" alt="Photo Share">
    </div>
  </div>
</section>

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

The jQuery functions properly offline, but fails to load when connected to the

I am encountering an issue with the following script: <script type="text/javascript"> $.getJSON("http://api.twitter.com/1/statuses/user_timeline/koawatea.json?count=1&include_rts=1&callback=?", function(data) { $("#headertweet") ...

Mobile version experiencing issue with Navbar not collapsing

The dropdown navigation bar on the mobile version of my website is not functioning properly. Despite several attempts, I have been unable to figure out a way to make it work effectively. <!DOCTYPE html> <html lang="en"> <head> & ...

Obtain the complete shipping address with the 'addressLines' included using Apple Pay

Currently working on integrating Apple Pay WEB using JS and Braintree as the payment provider. In order to calculate US sales tax for the order, I need to gather certain information. The user initiates the payment process by clicking on the "Pay with Appl ...

Chrome browser not responding to jQuery .html refresh

I am facing an issue with a page containing a list of tasks, each with a dropdown menu featuring a list of teams to which the task can be assigned. However, pre-populating the dropdown in the Action function is not feasible due to the large number of tasks ...

Utilizing JavaScript For Loops for Code Repetition

Apologies for the ambiguous question title - struggling to articulate this properly. Essentially, I have some JavaScript code that I am looking to streamline by using a for loop. $('.q1').keyup(function () { if ($.inArray($(this).val().toLo ...

Is it possible to create a single button that, upon clicking, fades in one image while simultaneously fading out another?

My goal is to have the blue square fade in on the first button click, then fade out while the red square fades in on the second click. Unfortunately, it seems that my current code is not achieving this effect. I'm open to any suggestions or help on h ...

When deploying a project built with Parcel v2 to GitHub Pages, the static image assets may not show up properly on the live site

My project website is hosted on . The images in question can be viewed by clicking on these containers: https://i.stack.imgur.com/F71OP.png https://i.stack.imgur.com/DvUgh.png When deploying my project to gh-pages, I encountered errors with dynamically ha ...

Alert message in jQuery validation for the chosen option value

I am attempting to validate a combo box with the code provided below. Currently, I receive multiple alert messages even if one condition is true. My goal is to only display one alert message when a condition is met and highlight the other values in red. Th ...

Unable to view post in a single page

Having trouble with my Wordpress blog on mobile. I can't seem to open posts in a single page, as there is no response when clicking or touching the post on the main page. The div class for the main page post is entry-content. Here are some styling co ...

Formik button starts off with enabled state at the beginning

My current setup involves using Formik validation to disable a button if the validation schema is not met, specifically for a phone number input where typing alphabets results in the button being disabled. However, I encountered an issue where initially, ...

Utilizing PHP configurations in JavaScript with AJAX for JSON implementation

Trying to have a config.inc.php file shared between PHP and JavaScript seems to work, but when using ajax, the "error-function" is always triggered. Is there a way to successfully share the config file with working ajax implementation? This is being utili ...

Are there any JavaScript alternatives to Python's pass statement that simply performs no action?

Can anyone help me find a JavaScript alternative to the Python: pass statement that does not implement the function of the ... notation? Is there a similar feature in JavaScript? ...

bootstrap 5 with uneven spacing

I am working on some HTML/CSS code that utilizes Bootstrap 5.2 and the justify-content-between class in order to move two buttons to the edges of a container. <div class="container"> <div class="row"> <div cla ...

Is the value of the object in the array already present in another array of objects?

Plunker - http://plnkr.co/edit/jXdwOQR2YLnIWv8j02Yp In my angular project, I am working on a view that displays a list of users in the main container (array-A) and a sidebar with selected users (array-B). The first array (A) contains all users: [{ $$has ...

Printing specific div content from an HTML page using a print button

I have a situation where I need to control the printing functionality of my HTML page. Currently, I have two div tags in my code with a single print button at the top of the page. When I click on this button, it prints the content from both divs. However ...

React's componentDidUpdate being triggered before prop change occurs

I am working with the CryptoHistoricGraph component in my app.js file. I have passed this.state.coinPrices as a prop for this element. import React from 'react'; import axios from 'axios'; import CryptoSelect from './components/cry ...

Can the method through which a controller was accessed be identified in asp MVC?

Is it possible for a controller method to handle different return types based on how it is called, such as returning a view when accessed restfully and returning a JsonResult when invoked via JavaScript? I am looking for a way to achieve this without dupli ...

Is there a way to achieve horizontal alignment for the Twitter and Facebook buttons on my page?

By utilizing the html code provided, I successfully incorporated Twitter and Facebook "Like" buttons into my website. Initially, they were stacked vertically, which resulted in excessive use of vertical space. To optimize space usage, I decided to align th ...

Utilize a JSON object with ChartJS for data visualization

Recently, I've been working with a REST API that returns historical data in the following format: { "2021-6-12": 2, "2021-6-13": 3, "2021-6-14" :1 } This data represents the count of measurements taken on each date ...

What is the importance of setting up an HTTP server?

Can anyone explain why, in nodeJs programming with javascript, it is necessary to establish a server using the http module, even though one could simply utilize the fs module for reading, writing, and updating data stored in a json file? ...