Utilizing the power of jQuery's tabify feature to implement a dynamic tab menu on a website (combining HTML and CSS

Trying to incorporate a tab navigation menu.

Came across this fantastic fiddle example http://jsfiddle.net/S78Bt/3/ that perfectly demonstrates what I want to achieve. However, I'm facing some difficulties in getting the tabify function to work.

Followed this link for assistance: Programmatically change tab using tabify jquery plugin

In the linked question, it was mentioned that the tabify function is no longer supported, so we have to manually code it out. It seems that I might have made a mistake while integrating the JS with my webpage as the tabs are unresponsive.

Sharing my code snippets below:

JS script:

<script>
(function(a){a.fn.extend({tabify:function(e){function c(b){hash=a(b).find("a").attr("href");return hash=hash.substring(0,hash.length-4)}function f(b){a(b).addClass("active");a(c(b)).show();a(b).siblings("li").each(function(){a(this).removeClass("active");a(c(this)).hide()})}return this.each(function(){function b(){location.hash&&a(d).find("a[href="+location.hash+"]").length>0&&f(a(d).find("a[href="+location.hash+"]").parent())}var d=this,g={ul:a(d)};a(this).find("li a").each(function(){a(this).attr("href", a(this).attr("href")+"-tab")});location.hash&&b();setInterval(b,100);a(this).find("li").each(function(){a(this).hasClass("active")?a(c(this)).show():a(c(this)).hide()});e&&e(g)})}})})(jQuery);

$(document).ready(function(){
        function getHref(el){
            hash = $(el).find('a').attr('href');
            hash = hash.substring(0,hash.length-4);
            return hash;
        }

        function setActive(el){         
            $(el).addClass('active');
            $(getHref(el)).show();
            $(el).siblings('li').each(function(){
                $(this).removeClass('active');
                $(getHref(this)).hide();
            });
        }

        $('#ulaa').tabify();
        setActive($('#target'));
    });
</script>

And here's the UL containing the tabs:

<ul id="ulaa"> 

   <li class="active"><a href="#a">abcd</a></li>
   <li id="target" ><a href="#bb">asdbk</a></li>
   <li><a href="#c">texte</a></li>
   <li><a href="#">foo</a></li>
   <li><a href="#">inserttexthere</a></li>

  </ul> 
 <div class="contenta" id="a">
   jQuery is a cross-browser…
  </div>
  <div class="contenta" id="bb">
   The Prototype JavaScript…
  </div>
  <div class="contenta" id="c">
   Ext (X-t) is a JavaScript…
  </div>

This setup exists within a single HTML file. Is there an additional step I need to take?

The desired functionality should be that upon clicking on a tab, the corresponding tab gets the 'active' class while removing the same from the previously active tab. Additionally, a div associated with the ID and the href of the tab should be displayed beneath the horizontal tabs.

Can anyone point out what's amiss with this code?

Answer №1

I implemented the code below on a web page and uploaded it to my site, resulting in functionality identical to that of the fiddle.

Should jQuery not be included in the header, the page will display similar to what you are experiencing.

CODE

<!DOCTYPE html>
<head>

<title>Sonnet OneSonnet TwoSonnet</title>

    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"/>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<style>
body { font: 0.8em Arial, sans-serif; }
.menu { padding: 0; clear: both; }
.menu li { display: inline; }
.menu li a { background: #ccf; padding: 10px; float:left; border-right: 1px solid #ccf; border-bottom: none; text-decoration: none; color: #000; font-weight: bold;}
.menu li.active a {
    background: #eef;
}
.content { float: left; clear: both; border: 1px solid #ccf; border-top: none; border-left: none; background: #eef; padding: 10px 20px 20px; width: 400px; }

</style>
</head>
<body>

<div class='mytabs'>
  <ul id="menu" class="menu">
    <li><a href="#description">Sonnet One</a></li>
    <li><a href="#usage">Sonnet Two</a></li>
    <li><a href="#download">Sonnet Three</a></li>
  </ul>
<div id="description" class="content">
    <h2>Sonnet One</h2>
    <p>
        When forty winters shall besiege thy brow,
        And dig deep trenches in thy beauty's field.
    </p>
</div>
<div id="usage" class="content">
    <h2>Sonnet Two</h2>
    <p>
        But I do sing once more and value still,
        For love of me whom must myself forsake
    </p>
</div>
<div id="download" class="content">
    <h2>Sonnet Three</h2>
    From fairest creatures we desire increase,
    That thereby beauty's rose might never die,
</div>

</div>
<script>
$('.mytabs').tabs()

$( '.menu li a' ).on('click', function(){
    $( '.menu li a' ).css('background-color', '#ccf');
    $( this ).css('background-color', 'green');
});
</script>
</body>

</html>

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 functionality of triggers is not applicable to content that is dynamically loaded through ajax requests

I encountered a problem recently that required me to rewrite some functions. The original code looked like this: $('#type_of').on('change', function () { However, due to issues with manipulating the DOM and loading content via ajax, I ...

Using React components with Material UI to create styled div boxes

As I work on creating a mock website with React and Material UI, I have utilized the RaisedButtonExampleSimple component for buttons from Material UI within a div named App-Intro. Strangely, the text in the Div "Customer-Choice" is displaying above the but ...

positioning a text input in the center instead of at the top of the page

Hello everyone, I am just starting out with HTML and I have a question. How can I make the Username: <input type="text" name="Username" style="- margin-top: 200px;"> appear in the center of the page instead of at the top? I've tried changing the ...

I'm struggling to concentrate and unable to type in the email field

Today while working on a website, I encountered something strange. In the footer section of the site, there is a quick contact form. However, I noticed that in Firefox, I am unable to focus on the email field. Surprisingly, this issue does not occur in Chr ...

Ways to switch sidebar menu using only CSS?

How do I create a BS4 sidebar menu that toggles on click and replaces an icon? See the code below: .toggle { display: none; } .menu-head label { color: black; cursor: pointer; } .container { overflow: hidden; display: inline-block; } .si ...

search engine optimized for easy input via keyboard

I have successfully incorporated AJAX search functionality into my project, inspired by this example here. The unique feature of this implementation is the ability to navigate through search results using the TAB key. The structure of my search results tab ...

Tips on making a fresh form appear during the registration process

After clicking on a submit button labeled as "continue" within a form, a new form will appear for you to complete in order to continue the registration process. ...

Ways to verify various values in the toBe function

I'm currently testing to ensure that all the links display the correct text. I am specifically checking for two values: "enable" or "disable". expect(toggleName).toBe('Disable'); Is there a way to check for multiple values, such as 2 or mo ...

Tips on importing anime js after it has been successfully installed through npm

Today, I added anime js as a dependency to my package.json file. The version 3.0.1 is visible in the dependencies list. I used npm to install it. Next step was creating a folder and a JavaScript file in the public directory. I set up a simple event liste ...

Creating Dynamic Links with Ajax Using jQuery in HTML

Is there a way to set up an ajax link that triggers when clicking on any href link, input field, or button? ...

What are the steps for submitting a form with AJAX?

I am having trouble submitting a form via ajax. I am not receiving any error messages even though I have set up error handling throughout the website. Here is my code: <script type="text/javascript" src="jquery.js"></script> <div id="shower ...

Attempting to establish a cookie from the server end, however, it is not being successfully set on my client

Attempting to set a cookie on the client browser from the server side using Node.js and Express. When signing up, the cookie is properly sent in the response object but not being set on the client browser. Additionally, when trying to access a protected AP ...

Achieving responsive masonry layout with Bootstrap 4

I'm attempting to implement the bootstrap 4 masonry effect on my website, but I'm facing issues with card responsiveness. The page is very basic without any special effects. While the page works well when resizing the browser window, it doesn&ap ...

iOS Safari browser does not support changing the caret color in textarea

Seeking a solution to hide the text cursor (caret) from a textarea on iOS browsers like Safari and Chrome. Despite trying the caret-color property, it does not seem to work. Are there any alternative methods to achieve this? One approach I attempted is b ...

Developers specializing in Google Maps navigate to a particular destination

I have been working on an app that provides users with the best options for places to visit, utilizing Google Maps technology. Here is what I have accomplished so far: Show the user their current location Show the user possible destinations (with marker ...

Snowflake Javascript function for adding current date to table name

I've recently delved into the world of Snowflake and hit a roadblock with this issue: My goal is to duplicate a table named AB_USER to AB_USER_(current_date). Here's the code I've come up with in an attempt to achieve that: CREATE or repl ...

The contenteditable attribute does not function properly in a foreignobject element on Firefox browsers

<?xml version="1.0" standalone="yes" ?> <svg xmlns="http://www.w3.org/2000/svg"> <foreignObject width="100" height="100" x="20" y="65"> <body xmlns="http://www.w3.org/1999/xhtml"> <p contenteditable="true">text&l ...

Ways to remove information from an HTML form using PHP and MySQL

I'm a beginner in PHP and I've been learning how to add data, but I'm facing difficulties when it comes to deleting data from an HTML form. Here's the code I have so far: Database Name: mydb Table Name: registered HTML <html> ...

Incorporating an additional HTML form to the webpage

I've written some code and I'm looking to add another medicine when the + button is clicked. How can I achieve this? <html> <body style="color:black; font-size:20px;"> <form> <label style="margin-right:95px ...

searchkit - Issue with Maintaining State of RefinementListFilter Checkboxes

I was curious about the functionality of the RefinementListFilter in searchkit. I have multiple filters that are boolean values, such as {field: 'hasChildren': {'1' : 'Yes', '0': 'No'}} to illustrate the tr ...