Designating the selected button as "active"

Consider the following HTML snippet:

<div class="wp-editor-tabs"><button type="button" id="bbp_topic_content-tmce" class="wp-switch-editor switch-tmce" data-wp-editor-id="bbp_topic_content">Visual</button>
<button type="button" id="bbp_topic_content-html" class="wp-switch-editor switch-html" data-wp-editor-id="bbp_topic_content">Text</button>
</div>

View the fiddle here: Fiddle

Here is an image of how they appear:

https://i.sstatic.net/Wpkmu.png

Is it feasible to have a visual indicator when one button is clicked, such as a thicker border, and then have that change when the other button is clicked? Can this be achieved using CSS?

Moreover, upon page load, is it possible for one of the buttons to already appear as active? These buttons are used in a bbPress support forum to toggle between two editors.

Answer №1

Using CSS only

.wp-switch-editor:focus-within {
    border: thin solid black;
}
<div class="wp-editor-tabs"><button type="button" id="bbp_topic_content-tmce" class="wp-switch-editor switch-tmce" data-wp-editor-id="bbp_topic_content">Visual</button>
<button type="button" id="bbp_topic_content-html" class="wp-switch-editor switch-html" data-wp-editor-id="bbp_topic_content">Text</button>
</div>

Using jQuery

$('.wp-switch-editor').on('click', function(){
  $('.wp-switch-editor').removeClass('active');
  $(this).addClass('active');
});
.active {
  border: 1px solid #000;
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wp-editor-tabs"><button type="button" id="bbp_topic_content-tmce" class="wp-switch-editor switch-tmce" data-wp-editor-id="bbp_topic_content">Visual</button>
<button type="button" id="bbp_topic_content-html" class="wp-switch-editor switch-html" data-wp-editor-id="bbp_topic_content">Text</button>
</div>

Answer №2

To achieve the desired behavior, consider using javascript or jQuery instead of relying on :focus which may not give you the expected results.

.wp-editor-tabs button:focus {
  border:3px solid #000;
}
<div class="wp-editor-tabs">
  <button type="button" id="bbp_topic_content-tmce" class="wp-switch-editor switch-tmce" data-wp-editor-id="bbp_topic_content">Visual</button>
  <button type="button" id="bbp_topic_content-html" class="wp-switch-editor switch-html" data-wp-editor-id="bbp_topic_content">Text</button>
</div>

Answer №3

HTML

<div class="wp-editor-tabs"><button type="button" id="bbp_topic_content-tmce" class="btn wp-switch-editor switch-tmce" data-wp-editor-id="bbp_topic_content">Visual</button>
    <button type="button" id="bbp_topic_content-html" class="btn wp-switch-editor switch-html" data-wp-editor-id="bbp_topic_content">Text</button>
    </div>

CSS

button {
  border: none;
  outline: none;
  padding: 10px 16px;
  background-color: #f1f1f1;
  cursor: pointer;
  font-size: 18px;
}

/* Style the active class, and buttons on mouse-over */
.active, .btn:hover {
  background-color: #666;
  color: white;
}

JS

// Locate the container element
var btnContainer = document.getElementById("myDIV");

// Find all buttons with class "btn" inside the container
var btns = btnContainer.getElementsByClassName("btn");

// Iterate through the buttons and apply the active class to the current/clicked button
for (var i = 0; i < btns.length; i++) {
  btns[i].addEventListener("click", function() {
    var current = document.getElementsByClassName("active");
    current[0].className = current[0].className.replace(" active", "");
    this.className += " active";
  });
}

Answer №4

If you want to achieve this using Jquery, here's how:

<style>
  .button{
    border:1px solid blue;
    height: 50px;   
    margin:10px;
  }
</style>

<button id="button1" class="button">Visual</button>
<button id="button2" class="button">Text</button>

<script>
   $('.button').click(function(){
       $(this).parent().find('.button').css('background-color','#ffffff');
       $(this).css('background-color','#ff0000');
   });
</script>

http://jsfiddle.net/Jeagerck/1v7h3szc/5/

Answer №5

var button = document.querySelectorAll("#bbp_topic_content-tmce")[0];
button.addEventListener("click", function(event) {
    var $classList = event.currentTarget.classList;
  if(!$classList.contains("clicked")) {
    $classList.add("clicked");
  } else {
    $classList.remove("clicked");
  }

});

var button = document.querySelectorAll("#bbp_topic_content-tmce")[0];
button.addEventListener("click", function(event) {
var $classList = event.currentTarget.classList;
  if(!$classList.contains("clicked")) {
  $classList.add("clicked");
  } else {
  $classList.remove("clicked");
  }

});
.clicked {
  background: red;
}
<div class="wp-editor-tabs"><button type="button" id="bbp_topic_content-tmce" class="wp-switch-editor switch-tmce" data-wp-editor-id="bbp_topic_content">Visual</button>
<button type="button" id="bbp_topic_content-html" class="wp-switch-editor switch-html" data-wp-editor-id="bbp_topic_content">Text</button>
</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

The slider thumb on materialized CSS HTML5 Range is not visible

After updating to Materialize 1.0.0, I noticed that the thumb is not showing on the HTML5 range input. Interestingly, when using Materialize 0.100.2 or 0.97.3, the thumb is displayed correctly. Unfortunately, I can't revert back to the older version ...

Convert the button element to an image

Can someone please explain how to dynamically change a button element into an image using javascript when it is clicked? For instance, changing a "Submit" button into an image of a check mark. ...

Is there a way to modify the button's color upon clicking in a React application?

I am a beginner in the world of React and currently exploring how to utilize the useState hook to dynamically change the color of a button upon clicking. Can someone kindly guide me through the process? Below is my current code snippet: import { Button } ...

Within a div element, there are links that I would like to hover over to emphasize the text

Hey there, I'm looking to make the text change color when you hover over the entire div. Currently, only the background changes color, but I want the text to as well. Thank you for your assistance! .shadow-box .service-box {text-align:center;borde ...

How to style two distinct elements within an HTML select tag option

Within my PHP script, I am retrieving a table with two columns from the database and presenting them in a dropdown list. Below is the code snippet: echo "<select id='isv' class='required'>"; while($row = pg_fetch_row($result)){ ...

Is there a way for me to automatically update a webpage every 5 minutes, beginning at 8:01 a.m., and subsequently at 8:06 a.m., and so forth?

<html> <head>Harshal</head> <script> var limit="5:0" var doctitle = document.title var parselimit=limit.split(":") parselimit=parselimit[0]*60+parselimit[1]*1 function beginrefresh(){ if (parselimit==1) window.location.reload ...

ReactJS not displaying the class effect as intended

When using the react zoom pan pinch library, I am trying to set the height to "100%" for TransformWrapper and TransformComponent. Interestingly, it works perfectly fine when done through Chrome inspect, but when attempting to add a className or use style={ ...

Empty vertical space on the right side in iPad view

I am currently working on the development of this website and facing an issue. https://i.sstatic.net/Y7B7Q.png When switching to iPad view in Chrome's developer mode, I noticed a blank column on the extreme right side that spans the entire page. I a ...

Preselecting items in PrimeNG Checkbox: A step-by-step guide

How can I ensure that the already selected item is displayed upon loading this div? The code in `rp` consists of an array of type Permission with one element, which should be automatically selected. What could be causing the issue? Here is the HTML snippe ...

Efficiently Filtering User Input and Other Things

Imagine I have a setup for organizing a television guide like this: <div class="day"> <p>A Date</p> <div class="time"> <p>A Time</p> <div class="show"> <p>A Show</p> ...

What is the best way to send a parameter to a component in Vue.js without including it in the URL during routing?

One of my Vue.js components is a SideBar that retrieves JSON data. The JSON data consists of names that correspond to specific URLs which in turn contain more JSON data. { "applicants": "url1", "applications": "url2" ...

Adjust the section and aside elements to automatically expand when used within the main tag

Is there a way to ensure that the inner-section and sidebar have a minimum height, while also expanding vertically along with the main tag? Here is an example of the HTML code: <body> <header></header> <main> <s ...

The hidden absolute positioned div disappears once the sticky navbar becomes fixed on the page

Whenever my navbar reaches the top of the screen, the links in the dropdown menu disappear. I followed tutorials and examples from w3schools to build my webpage. Specifically: howto: js_navbar_sticky howto: css_dropdown_navbar This code snippet exempli ...

Is there a possibility of encountering problems when collapsing table rows versus individual cells?

By using the css visibility property, you have the ability to set it to collapse for specific table rows. Nevertheless, there are two methods to accomplish this task. The first is to implement the logic on the <tr> element: thead > tr { visi ...

Problem with using Twitter Bootstrap in IE11 when Browser Profile Enterprise is enabled

I am facing an issue with a Bootstrap 3 web page. Some machines have IE configured to load intranet pages in Enterprise profile mode, while others have the default Desktop profile set in IE11. This configuration has caused the layout to break completely. ...

View the picture directly on this page

Currently, I am in the process of creating a gallery and I would like the images to open on top of everything in their original size when clicked by the user. My expertise lies in HTML and CSS at the moment, but I am open to learning JavaScript and jQuery ...

Styling HTML elements for Google custom search API json results: Best practices

I have been working with the Google Custom Search API to retrieve JSON callback results for my project. I have been experimenting with the JSON callback variables based on the recommendations from Google's documentation available here: https://github ...

Selecting a "non-operational" collection location on an e-commerce platform

Recently I discovered a bug on an online shopping website. It seems that by using inspect element, it was possible to alter the HTML code and change an unavailable pickup point to available. This allowed me to place an order, make a payment, and even recei ...

The backtick is not functioning correctly when trying to append the result in the Internet Explorer browser

I am using the .html method to append HTML content to a specific div ID within my file. <html> <head> Title <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body> ...

Animation fails to initiate when the object enters the viewport

I attempted to inject some enchantment into my project by implementing code from a tutorial found on this CodePen. However, I encountered an issue where the code only functions properly within that specific CodePen environment. Even after copying the same ...