Implementing jQuery to showcase an element on the screen

I would like to express my gratitude to everyone who has helped me. Please forgive my poor English :)

Can someone provide me with a jQuery script that can extract the name of the currently selected tab and display it in an h1 tag? Whenever the selected tab changes, I want the content in the h1 tag to dynamically update.

For example, here is my current setup:

<div class="header-inner">
      <div class="cal-menu-na">
        <h1>Text Here</h1>
      </div>
    </div>
    <div class="navbar">
      <span class="sone active"><a href="javascript:void(0);" data-tab="one">One ABC</a></span>
      <span class="stwo"><a href="javascript:void(0);" data-tab="two">Two XYZ</a></span>
      <span class="sthree"><a href="javascript:void(0);" data-tab="three">Three QWE</a></span>
      <span class="sfour"><a href="javascript:void(0);" data-tab="four">Four FGH</a></span>
    </div>

    <div class="main-contain">
      <div class="tabContainer">
        <div id="one" class="Tabcondent active">Tab One</div>
        <div id="two" class="Tabcondent">Tab Two</div>
        <div id="three" class="Tabcondent">Tab Three</div>
        <div id="four" class="Tabcondent">Tab Four</div>
      </div>
    </div>

What I am aiming for is:

<div class="header-inner">
      <div class="cal-menu-na">
        <h1>One ABC ( or Two XYZ or Three QWE or Four FGH )</h1>
      </div>
    </div>
    <div class="navbar">
      <span class="sone active"><a href="javascript:void(0);" data-tab="one">One ABC</a></span>
      <span class="stwo"><a href="javascript:void(0);" data-tab="two">Two XYZ</a></span>
      <span class="sthree"><a href="javascript:void(0);" data-tab="three">Three QWE</a></span>
      <span class="sfour"><a href="javascript:void(0);" data-tab="four">Four FGH</a></span>
    </div>

    <div class="main-contain">
      <div class="tabContainer">
        <div id="one" class="Tabcondent active">Tab One</div>
        <div id="two" class="Tabcondent">Tab Two</div>
        <div id="three" class="Tabcondent">Tab Three</div>
        <div id="four" class="Tabcondent">Tab Four</div>
      </div>
    </div>

You can find a full demo at: https://jsfiddle.net/zh8rqnkd/

Answer №1

Utilize this code to change the content of a clicked tab to match that of an h1 element:

$(this).closest(".navbar").prev(".header-inner").find("h1").text($(this).text());

View Demo


  • closest locates the first parental element matching the criteria.
  • prev retrieves the immediate previous sibling, excluding text nodes.
  • find finds descendants that match the specified selector.

This snippet is useful for updating text in multiple instances of similar HTML structures. However, if you only have one instance of the structure, you can simplify by using

$("div.header-inner > .cal-menu-na > h1").text($(this).text())
.

Answer №2

To change the text in the 'h1' element to the selected text from the 'a' element, simply use the .text() method within a click event:

$('.cal-menu-na h1').text($(this).text());

Check out the demo here!

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

Is it possible to invoke the $.Widget.destroy() method from jQuery's $.widget.bridge()?

I am trying to utilize jQuery's $.widget.bridge(); function and I am having trouble figuring out how to properly call the $.widegt.destroy() method in order to destroy widget instances. // Bridging to our FileSelect plugin. $.widget.bridge( "fileSele ...

Display or Conceal Multiple Divisions Using Angular 2

I am looking to implement a functionality using Li lists to toggle the visibility of multiple divs in Angular 2. Initially, all divs on the page will be visible. When viewing on a smaller screen, I want to hide some divs and show a specific div when a cor ...

Is there a feature in jQuery's Ajax that functions similarly to .NET's LoadingElementId?

The Ajax helper in .NET comes with the LoadingElementId: String property, which allows you to specify the ID of the DOM element that will be displayed while the request is being processed. I am seeking a simple solution to add an 'In Process' sp ...

Utilize MaxMind GeoIP2 to identify the city accurately

I have been using this simple code to retrieve the city name through the maxmind service (geoip2). It has been working flawlessly and I am grateful to a fellow member of this site who shared this code with me. However, there is an issue when the user&apos ...

Safari (mac) experiencing issues with loading material icons properly on initial attempt

Upon my initial visit to the website, I noticed that the font appeared distorted. https://i.sstatic.net/BtUxI.png However, when I right-clicked and chose "reload page", the fonts were displayed correctly. https://i.sstatic.net/3XUcA.png This issue seem ...

Update the icon using CSS style properties

I have been using liqour-tree and I need to change the arrow icon to a custom one. Unfortunately, I am not sure how to do this. Can someone please help me out? I have identified the element tag where the icon is placed. <i class="tree-arrow expanded ha ...

Using Javascript and JQuery to create an alert that pops up upon loading the page is not effective

I am having trouble making an alert show up when my website loads. The Javascript code is functional when directly included in the HTML, but once I move it to a separate file named script.js and link it, nothing happens. Can someone please help me identify ...

Utilizing Bootstrap to create a responsive design with a full-height view on the body,

While adjusting the page width in Chrome dev tools, I noticed that the page also increases vertically. It appears that Bootstrap is assuming a viewport height larger than what it actually is. Here is the relevant code snippet: <!DOCTYPE html> <ht ...

Utilize Jquery Loop to Showcase JSONP Array

I've gone through a multitude of similar questions, attempted to implement the solutions provided but still can't quite seem to make it work. I'm pretty sure it's just a simple mistake since I'm new to this. I have removed the URL ...

Guide to invoking a jQuery function by clicking on each link tab

Below is a snippet of jQuery code that I am working with: <script> var init = function() { // Resize the canvases) for (i = 1; i <= 9; i++) { var s = "snowfall" + i var canvas = document.getElementById( ...

how to use serializeArray() to create an array with named keys

I am struggling with a piece of HTML code : <input type="checkbox" name="search-form[filters][category][12]" value="cat-12" autocomplete="off" checked="checked" /> <input type="checkbox" name="search-form[filters][category][14]" value="cat-14" au ...

Issues with styling classes using global SCSS in React are causing unexpected behavior

Currently, I am working on a React project that includes the use of SASS for styling purposes. While implementing modular CSS in my components, I have encountered an issue with applying styles from a main.scss file in my Layout.js component. The unique a ...

Make the Angular Modal scrollable except for when a specific div is being click dragged

I am working on an Ionic app that uses AngularJS. One issue I encountered is with a modal popup that can sometimes extend beyond the visible area. To address this, we applied overflow: hidden to its CSS class. However, there is a specific functionality i ...

Tips for aligning navbar content in the center using Bootstrap

I'm currently working on creating a Bootstrap 4 navbar, but I'm having trouble positioning objects within the navbar. I want to center a search box and place a button on the right, but so far I have not been able to achieve this as the navbar is ...

Center an image both vertically and horizontally within an Owl Carousel arrow slider

I have set up an owl carousel on my page with a customized arrow positioned next to the slider. Currently, it looks like this: https://i.stack.imgur.com/gCfM0.png My goal is to vertically align the arrows and make sure the left arrow is aligned to the le ...

Displaying an error message: Uncaught ReferenceError - Undefined reference detected

Hi there, I am having some trouble with a JSON file and I would appreciate some help. The error message I am receiving is: Uncaught ReferenceError: marketlivedata is not defined <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquer ...

Prevent submitting a form multiple times with jQuery Validate - ensuring successful submission only once

I've been working on updating my Email Contact form with the jQuery Validate Plugin and AJAX within the submitHandler. It initially worked, but I'm running into an issue where it only works once. Upon trying to submit the form again, I stop recei ...

Looking to retrieve just your Twitter follower count using JavaScript and the Twitter API V1.1?

I am trying to retrieve my Twitter follower count using JavaScript/jQuery in the script.js file and then passing that value to the index.html file for display on a local HTML web page. I will not be hosting these files online. I have spent weeks searching ...

My goal is to retrieve and print the duplicated values only once from an associative array

Given an associative array, I need to print all the department names without any repetitions. <h3>2. List out all department names</h3> <div class="all"> </div> Here is my JavaScript code: var employee=[{"firstName":"Zahir","last ...

What could be causing the problem with my CSS background-image being referenced correctly?

Everything seems to be in order: background-image: url(./dir/img.jpg); However, I encounter a 404 error when attempting to reference an image using this approach: index.html <div style="--url: url(./dir/img.jpg);"></div> css backg ...