Pressing a button triggers the highlighting of a tab in HTML through the use of Javascript

In the corner of my webpage, I have three tabs: info, question, order. When I click on one tab header, only that tab should highlight. The info section includes two buttons that link to the question and order tabs. When these buttons are pressed, the respective tab is displayed correctly but doesn't highlight as intended.

If you want to see the issue in action, please visit this URL where you can find the 3 tabs on the right side of the page:

I attempted to use a Bootstrap code to solve the problem, but it wasn't very successful.

Your help with this matter would be highly appreciated!

<script>
function openProductTab(evt, tabName) {
    var i, tabcontent, openProductTab;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    tabProductLinks = document.getElementsByClassName("tabProductLinks");
    for (i = 0; i < tabProductLinks.length; i++) {
        tabProductLinks[i].className = tabProductLinks[i].className.replace(" active", "");
    }
    document.getElementById(tabName).style.display = "block";
    evt.currentTarget.className += " active";
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
/*----------------------*/
/*---Produkttabbar------*/
/*----------------------*/

.tabProdukter {
 overflow: auto;
}

/*----------tab button---------*/


.tabProducts button{
  color:#1e84d8;
  /*margin-bottom: 0 px;*/
    background-color: #e7f6ff;
    float: left;
    cursor: pointer;
    transition: 0.1s;
  width:33%;
border-top: 1px solid #ebebeb;
  border-left: 1px solid #ebebeb;
  border-right: none;
border-bottom: 1px solid #20a3eb;
}

.tabProducts button:hover{
color: white;
  background-color:#20a3eb;
}

.tabProducts button.active {
color:#1e84d8;
width: 33%;
background-color: white;
border-top: 1px solid #20a3eb;
border-left: 1px solid #20a3eb;
border-right: 1px solid #20a3eb;
border-bottom: none;
}

.tabProducts button.active:hover{
color: #20a3eb;
}

/* Style the tab content */
.tabcontent {
  display: none;
    padding: 0px 0px;
}


.tabcontent ul{
margin-left:0px;
}

.tabcontent li{
list-style:none;

}
.tabcontent h3{
border-bottom:dotted;
border-bottom-width: 1px;
border-color: #20a3eb;
text-align:left;
padding-top:20px;
padding-bottom:5px;
margin-bottom:5px;
  font-size:18px;
}

.tabcontent h4{
  color:#515151;
}
<div class="tabProducts" id="ProductInformationTabs">
  <button class="tabProductLinks" onclick="openProductTab(event, 'produktinformation')" id="defaultOpen">[icon name="info-circle" class="" unprefixed_class=""] Info</button><button class="tabProductLinks" onclick="openProductTab(event, 'flerFragor',)">[icon name="question-circle" class="" unprefixed_class=""] Frågor</button><button class="tabProductLinks" onclick="openProductTab(event, 'bestallNu')">[icon name="shopping-cart" class="" unprefixed_class=""] Beställ</button>
</div>

<div id="produktinformation" class="tabcontent">
<ul>
<li><h3>[icon name="info-circle" class="" unprefixed_class=""]  Produktinformation</h3></li>
<li>[icon name="clock-o" class="blue1ColorClass" unprefixed_class=""] Leveranstid: <span style="color: #1e84d8;">1-2 veckor</span></li>
<li>[icon name="arrow-circle-o-down" class="blue1ColorClass" unprefixed_class=""] Minsta antal: <span style="color: #1e84d8;">100 st</span></li>
<li>[icon name="adjust" class="blue1ColorClass" unprefixed_class=""] Färger: [icon name="circle" class="blueColorClass" unprefixed_class=""] [icon name="circle" class="redColorClass" unprefixed_class=""] [icon name="circle" class="greenColorClass" unprefixed_class=""] [icon name="circle" class="blackColorClass" unprefixed_class=""]</li>
<li><a href="http://webb.io/swebrush/wp-content/uploads/tandborstar_tryckmall_för_profiltryck.pdf">[icon name="download" class="blue2ColorClass alignright" unprefixed_class=""]</a> Mall för nedladdning: <a href="http://webb.io/swebrush/wp-content/uploads/tandborstar_tryckmall_för_profiltryck.pdf"><span style="color: #1e84d8;">Tandborstar.pdf</a></span></li>

</ul>
<ul>
<li><h4>Är du intresserad av att beställa produkten?</h4></li>
<li><button class="buttonDefault" onclick="openProductTab(event, 'flerFragor','defaultOpen')">Jag har fler frågor  [icon name="question-circle" class="" unprefixed_class=""]</button></li>
<li><button class="buttonOrderClass" onclick="openProductTab(event, 'bestallNu')">Beställ nu'   [icon name="shopping-cart" class="" unprefixed_class=""]</button></li>
</ul>
</div>

<div id="flerFragor" class="tabcontent">
<ul>
<li><h3>[icon name="question-circle" class="" unprefixed_class=""]  Jag har några frågor först</h3></li>
<li>Att beställa profilprodukter på webben är inte alltid en dans på rosor. Vilken tur att vi har duktiga personer som gärna assisterar dig i din beställning. Skicka in dina funderingar via kontaktformuläret så återkommer rätt person till dig! Du kan även ringa oss på , maila till <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1dac4dfd5c2d4c3c7d8d2d4f1c1c3d4dfd5de9fc2d4">[email protected]</a> eller skriva till oss direkt i chatten när den är bemannad.[contact-form-7 id="6" title="KONTAKTA OSS"]</li>
</ul>
</div>

<div id="bestallNu" class="tabcontent">
<ul>
<li><h3>[icon name="shopping-cart" class="" unprefixed_class=""]  Beställ nu</h3></li>
<li>Nu är du ett steg närmre! Välj önskad variant och antal och lägg sedan till produkten i varukorgen. Känner du dig osäker och behöver hjälp får du gärna kontakta oss.[contact-form-7 id="6" title="KONTAKTA OSS"]</li>
</ul>
</div>

Answer №1

Utilize classList for better results:

For addition:

evt.currentTarget.classList.add("mystyle");

For removal:

evt.currentTarget.classList.remove("mystyle");

For toggling:

evt.currentTarget.classList.toggle("mystyle");

To substitute:

evt.currentTarget.classList.replace( oldClass, newClass );

For additional information, visit this link.

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

Error 404: Node.js and Express encountering issues with POST request to '/webhook' endpoint

https://i.sstatic.net/CTDCv.pnghttps://i.sstatic.net/KjPeC.pngI've encountered a peculiar issue where I keep receiving a 404 error specifically when sending a post request to '/webhook'. This error is persistent even though the rest of my po ...

Tips for targeting a specific div element within a webview using code in Android app development

During my exploration of focusing on a webview in Android development, I encountered a question regarding setting focus on a div element. Despite making the div element editable, neither webView.requestFocus() nor document.getElementById('container&ap ...

Using j Query to create custom masks for various formats with the option to include optional digits

Looking for a way to mask the CVV card number..! The masking should allow for either 3 numbers like xxx 123 or 4 numbers like xxxx 4567 I have attempted to implement this with jQuery mask plugin, but it only allows for 4 characters. How can I enable both ...

The source for jQuery autocomplete is returning as null

Upon selection of a state and city by the user, an Ajax call is triggered to fetch data from the server. Once this data is retrieved, only then do I want to enable them to search through the results using jQuery UI autocomplete. I store the data in a varia ...

Unable to fetch the identification number from the database

I'm encountering an issue with retrieving the ID from my database: Here is a snapshot of my database: Below is my event controller class: <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Event ...

Unusual spacing issue observed between <div> elements on Internet Explorer, but not on Firefox or Opera

I am facing a common question that many others may have asked before, but I haven't been able to find a solution to my specific issue. When viewing my website on FF, Opera, and IE on Windows 7, everything displays correctly. However, when using IE7 o ...

The preflight response does not allow the access-control-request-methods request header field due to restrictions set in the access-control-allow-

I'm facing CORS issues while trying to make a POST request from my website to a remote server. Despite searching online, I couldn't find a solution that fits my specific problem. Below are the parameters for my ajax request: var params = { ...

Unable to generate a JSON string from the JavaScript object

I need help converting the object I receive from the server into a JSON file for use in a d3.js chart: data = { "dog ":"5", "cat ":"4", "fish ":"12", } The desired output is: { "name" : "animal", "children" : [ {"name":"dog" ...

How can I properly initialize React components?

I am currently learning React.js and experimenting with a progress bar animation. I stumbled upon this code that I would like to incorporate into my project, but I am unsure of where to place it. Check out the code here! The JavaScript code in question i ...

Debounce fails to honor the specified timeout period

I'm currently working on implementing a debounce function to handle an HTTP request function. The code is very similar to the example provided below, with only minor changes in function names. To start off, here's the debounce function I am usin ...

What are some best practices for utilizing `element.offsetParent` in HTML SVG elements?

I'm currently updating some JavaScript code that relies on the .offsetParent property. The recent changes in the application involve the use of SVG elements, which are causing issues with the JavaScript as mySvgElement.offsetParent always returns unde ...

Is there a way to execute a JavaScript function on a webpage using Selenium automation?

Here's an element on a website: <span class="log-out-ico" ng-click="logout()"> Instead of clicking it, I want to run the "logout()" script from selenium. Is that possible? If so, how can I do it? This is what I attempted: I ...

Enhance the spacing between the UL-LI navigation menu and the currently selected item

I'm struggling with adjusting the spacing between items in my navigation menu, specifically the li elements that are currently too close together. I've attempted to increase the margin-left within the .main_menu li CSS rule, but it doesn't s ...

Tips for modifying an HTML element's attribute when a button is clicked, both in the client and server side

Context: This question delves into the fundamental concepts of AJAX. I have been studying this tutorial along with this example for the JavaScript part, and this resource (the last one on the page) for the PHP segment. Imagine a scenario where a JavaScri ...

Verify the HTML embed code with either PHP, JavaScript, or the Zend PHP framework

I have a quick question: Are there any effective methods for validating an HTML embed code? I am currently using Zend Framework and prototype+scriptaculous and would appreciate any hints or tips related to this environment. Thank you! ^^ ...

Unit testing component in Ionic 2 with Ionic's specific markup and elements

Within my Angular 2 component for an Ionic 2 app, I utilize Ionic's markup as shown below: <ion-card> <h3>{{ rawcontent.name }}</h3> <p *ngIf="rawcontent.description">{{ rawcontent.description }}</p> </ion-car ...

Continuously send HTTP requests to the page until receiving a status code of 200, then proceed to redirect to the same page

I am new to JavaScript and seeking advice on how to tackle this task. I have attempted the following approach, but it is not functioning correctly: while (status !== 200) { checkPage(); } function checkPage() { var xhr = new XMLHttpReque ...

Enhance the functionality of Woocommerce email notifications by incorporating a customized VAT field

I have exhausted all options and tried various email hooks without success. I inherited an outdated PHP code that was developed by someone else, which I updated for new woocommerce hooks (since the code is 4 years old). Everything is functioning smoothly e ...

Can someone tell me what comes after my nextElementSibling in this specific scenario?

I am puzzled by the usage of nextElementSibling in this code. Can someone provide an explanation on what exactly it is and where it should be used? Thank you in advance! Also, my code seems to be malfunctioning as I keep receiving an error message that s ...

Adjust the Bootstrap Navbar by positioning one navigation item to the right side

I want to align the logout button to the right within my bootstrap navbar. https://i.stack.imgur.com/PDvSv.png Here is a demonstration of the current behavior: https://codepen.io/agrawalo/pen/mdbKYLX Code: <nav class="mb-1 navbar navbar-expand-sm na ...