Issues arise with tabbed content as default content fails to display and classes are not added upon clicking

I'm currently working on a tabbed module that consists of three tabs.

Here's how it operates: When the user clicks on a carousel_element, it reveals carousel_hidden-text within that div and displays it in another div called carousel_quote.

I'm facing some uncertainties:

  1. The default content is not pre-loaded in carousel_quote. In the example below, you can see that the red box is empty. I want it to automatically display the second carousel_element (lorum ipsum 2) by default.
  2. I also want the border on the second carousel_element to be red by default, indicating that this tab is active. To achieve this, I used
    jQuery('."carousel_element:nth-child(2)').addClass("carousel_selected");
    but it doesn't appear to have any effect. Additionally, whenever another tab is clicked, I need to remove carousel_selected from the previous one and add it to the appropriate carousel_element.

Sample Code:

jQuery(document).on('click', '.carousel_element', function() {
  jQuery('.carousel_quote').html(jQuery(this).find('.carousel_hidden-text').html());
  jQuery('."carousel_element:nth-child(2)').addClass("carousel_selected");
  jQuery('.carousel_element').removeClass('carousel_selected');
  jQuery(this).addClass('carousel_selected');
});
.container {
  margin-right: auto;
  margin-left: auto;
  padding-left: 15px;
  padding-right: 15px;
  padding: 0;
  margin-top: 0;
  position: relative;
  color: #002f33;
  width: 100vw;
}

.carousel {
  display: flex;
  flex-direction: row;
}

.carousel_element {
  border-bottom: solid 1px #999;
  height: 6em;
  width: 100%;
  margin: 0 5px;
}

.carousel_element .carousel_hidden-text {
  /* display: none; */
  text-align: center;
}

.carousel_container .carousel_quote {
  margin-left: auto;
  margin-right: auto;
  height: 10em;
  width: 75%;
  text-align: center;
  margin-top: 1em;
  border: 1px solid red;
}

.carousel_selected {
  border-color: red!important;
}
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="container carousel_container">
  <div class="wrapper">

    <div class="carousel">
      <div class="carousel_element">
        <div class="carousel_hidden-text">
          <p>Lorum Ipsum 1 </p>
          <p class="carousel_reference">Author1 </p>
        </div>
      </div>
      <div class="carousel_element">
        <div class="carousel_hidden-text">
          <p>Lorum Ipsum 2</p>
          <p class="carousel_reference">Author 2</p>
        </div>
      </div>
      <div class="carousel_element">
        <div class="carousel_hidden-text">
          <p>Lorum Ipsum 3</p>
          <p class="carousel_reference">Author 3</p>
        </div>
      </div>
    </div>
    <!-- .carousel end -->
    <div class="carousel_quote"></div>
  </div>
</div>

Answer №1

Make sure to include the following function for on load event:

$(document).ready(function () {
  $('.carousel_element:nth-child(2)').addClass("carousel_selected");
   $('.carousel_quote').html($('.carousel_element:nth-child(2)').find('.carousel_hidden-text').html());
});

Feel free to try out the solution provided below, it should work as expected.

$(document).ready(function () {
  $('.carousel_element:nth-child(2)').addClass("carousel_selected");
   $('.carousel_quote').html($('.carousel_element:nth-child(2)').find('.carousel_hidden-text').html());
   
$('.carousel_element').click(function() {
  $('.carousel_quote').html($(this).find('.carousel_hidden-text').html());
 
  //$('.carousel_element').removeClass('carousel_selected');
$(this).addClass("carousel_selected").siblings().removeClass("carousel_selected");

});
});
.container {
  margin-right: auto;
  margin-left: auto;
  padding-left: 15px;
  padding-right: 15px;
  padding: 0;
  margin-top: 0;
  position: relative;
  color: #002f33;
  width: 100vw;
}

.carousel {
  display: flex;
  flex-direction: row;
}

.carousel_element {
  border-bottom: solid 1px #999;
  height: 6em;
  width: 100%;
  margin: 0 5px;
}

.carousel_element .carousel_hidden-text {
  /* display: none; */
  text-align: center;
}

.carousel_container .carousel_quote {
  margin-left: auto;
  margin-right: auto;
  height: 10em;
  width: 75%;
  text-align: center;
  margin-top: 1em;
  border: 1px solid red;
}

.carousel_selected {
  border-color: red !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="container carousel_container">
  <div class="wrapper">

    <div class="carousel">
      <div class="carousel_element">
        <div class="carousel_hidden-text">
          <p>Lorum Ipsum 1 </p>
          <p class="carousel_reference">Author1 </p>
        </div>
      </div>
      <div class="carousel_element">
        <div class="carousel_hidden-text">
          <p>Lorum Ipsum 2</p>
          <p class="carousel_reference">Author 2</p>
        </div>
      </div>
      <div class="carousel_element">
        <div class="carousel_hidden-text">
          <p>Lorum Ipsum 3</p>
          <p class="carousel_reference">Author 3</p>
        </div>
      </div>
    </div>
    <!-- .carousel end -->
    <div class="carousel_quote"></div>
  </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

A guide to merging two JSON objects into a single array

Contains two different JSON files - one regarding the English Premier League stats for 2015-16 season and the other for 2016-17. Here is a snippet of the data from each file: { "name": "English Premier League 2015/16", "rounds": [ { "name": ...

Ensure you have the right zoom in and out percentages to properly test how your website responds on various browsers across different

Currently, I am using Google Chrome to test the responsiveness of a web application that I am developing. As of today, my browser versions are as follows: Google Chrome - Version 88.0.4324.96 (Official Build) (64-bit) Mozilla Firefox - Version 84.0.2 (64- ...

Trouble opening attached html file with Asp.net and Google Charts integration

I am facing an issue with my Asp.Net page that includes a grid and an image. The image is a Google Charts chart with a URL of approximately 1600 characters. To send this content as an email attachment, I created an .htm file containing the grid and the ima ...

Unable to locate the module model in sequelize

Having some trouble setting up a basic connection between Postgres and SQL using Sequelize. I keep getting an error where I can't require the model folder, even though in this tutorial he manages to require the model folder and add it to the sync, lik ...

Issue with parameter functionality not working as expected

This code snippet is not functioning as expected. I am trying to extract and print the values from the URL parameter file:///C:/Users/laddi/Desktop/new%201.html?t=vindu&b=thind function GetURLParameterValue(param) { var pageURL = window. ...

best practices for passing variables between multiple files within a nodejs application

// script.js var mydata = 1 module.exports = {mydata}; // file in my codebase var newData = require("../script.js") console.log(newData.mydata); // why is it undefined? I want to declare a variable as global across the entire project. I tried using ...

Can a Textmate snippet be activated using a Regex pattern instead of just a specific character?

Hey there, I've been spending a lot of time working with CSS recently, and I have to admit that constantly typing "px" after every width, height, margin, etc., is starting to get on my nerves. I find myself refining pixel values in Firebug quite ofte ...

Employ the power of a static force layout to forge an intricate web of connections within the realm of

I have found a great network that works really well. It automatically updates the node position when the size of the window changes. Now, I want to make it fixed. What I mean by this is that I want to compute a certain number of ticks (let's call it ...

Utilizing Ajax to dynamically update the color of a button when it is clicked from a different device

Currently, I am developing a program for my own use within my local community theater. The setup will involve a computer in the control room (Sound Booth) and another computer backstage. My plan is to create a grid of around 10 interactive "buttons" on th ...

Ant Design radio group buttons are currently dysfunctional

I'm having some trouble with radio group buttons. Can anyone assist me with this issue? (your help is greatly appreciated) Current Problem: Radio Group buttons are unclickable. Not sure where the issue lies! Technologies Used: React hooks, styled-co ...

Referencing an object by using a variable containing its name

I need a way to reference an object using a variable with its name in it. I've figured out how to do this for properties and sub-properties: var req = {body: {jobID: 12}}; console.log(req.body.jobID); //12 var subProperty = "jobID"; cons ...

There was a mistake: _v.context.$implicit.toggle cannot be used as a function

Exploring a basic recursive Treeview feature in angular4 with the code provided below. However, encountering an error when trying to expand the child view using toggle(). Encountering this exception error: ERROR TypeError: _v.context.$implicit.toggle i ...

What can be done to address the issue of v-model select option onchange displaying the previously selected value or becoming stuck on a static value rather than updating

Whenever I navigate to the message page and select a device, the v-model selected value changes. However, if I go to the device or contact page, the v-model selected value remains unchanged or goes back to the last selected value. Below is the function in ...

What is the best way to load a partial in Rails asynchronously with AJAX?

I am currently using the following code to load a partial when I reach the bottom of a div containing a table: $(document).ready(function () { $("#pastGigs").scroll(function () { if (isScrollBottom()) { $('#pastGig ...

Error message "Unexpected token" occurs when using jQuery bxSlider

Greetings to all, I have implemented jQuery bxSlider in my project and here is the code snippet: jQuery(document).ready(function() { jQuery('.abc').bxSlider(function() { mode: 'fade', ...

Having trouble with Typeahead Bootstrap not displaying dropdowns for ajax calls?

Utilizing Bootstrap 3 with the compatible Typeahead extension (https://github.com/bassjobsen/Bootstrap-3-Typeahead). The website operates on Django, and the query fetches information from a local Postgres server. Experiencing issues with displaying dropd ...

Clicking the checkbox does not synchronize with the table row

Is there a way to activate the button when the user selects the checkbox directly? Currently, the button only becomes enabled when the user clicks on a table row that is clickable. var btn = $("#btn"); btn.attr("disabled","disabled"); $("#table tbo ...

What is the most effective way to share data among components in React?

I recently delved into learning about react and find myself puzzled on how to pass data between two components. Presently, I have set up 2 functions in the following manner: First, there's topbar.tsx which displays information for the top bar, inclu ...

The local variable within the Angular constructor is not initialized until the ngOnInit() function is invoked

I am encountering difficulties with making backend calls from Angular. In my component, I am fetching the "category" parameter from the URL as shown below: export class ProductsComponent{ productList = [] category = "" $params; $products ...

Launching a single modal for multiple posts

I have a collection of posts and for each post, I want the ability to open a modal. However, I am looking for a solution where I can use a single dynamic modal instead of creating multiple modals for each post. Here is the code I currently have: https://j ...