Rewrite the title to: Focus on what the console buttons do, not other areas
P粉315680565
P粉315680565 2023-09-02 09:28:50
0
1
489
<p>I have 4 buttons and when they are clicked I am trying to set focus on them so that the user can see the currently active button. </p> <p>For some reason in the console it works as expected, when a button is clicked it adds focus, changes the background color based on CSS, then when another button is clicked the first button loses focus , the new button gets focus. </p> <p>It doesn't work when I don't have the console open. </p> <p> <pre class="brush:js;toolbar:false;">const oneYear = document.getElementById('1Year'); const fiveYear = document.getElementById('5Year'); const tenYear = document.getElementById('10Year'); const twentyYear = document.getElementById('20Year'); function changeDate(date) { if (date === 1) { oneYear.className = "active"; setTimeout(oneYear.focus(), 1); } if (date === 5) { fiveYear.focus(); fiveYear.className = "active"; } if (date === 10) { tenYear.focus(); } if (date === 20) { twentyYear.focus(); } }</pre> <pre class="brush:css;toolbar:false;">.theme-dark-btn { transition: all ease 1s; background-image: linear-gradient(to left, #1ACC8D, #1ACC8D, #235FCD, #1C4CA3); background-size: 300%; background-position: 0 0; border: 1px solid #1C4CA3; font-weight: 600; letter-spacing: 1px; } .theme-dark-btn:hover { background-position: 100% 0; border: 1px solid #1ACC8D; } .theme-dark-btn:focus { background-color: #1ACC8D; } .theme-dark-btn:active { background-color: #1ACC8D; } .btn { height: 38px; line-height: 35px; text-align: center; padding: 0 18px; text-transform: uppercase; transition: background-image .3s linear; transition: box-shadow .3s linear; -webkit-border-radius: 20px; -moz-border-radius: 20px; border-radius: 20px; font-size: 12px !important; } .btn:focus { background-color: #1ACC8D; }</pre> <pre class="brush:html;toolbar:false;"><div class="col"> <button class="btn theme-dark-btn" type="button" style="color: white" id="1Year" onclick="changeDate(1)" autofocus>1 Year</button> <button class="btn theme-dark-btn" style="color: white" id="5Year" onclick="changeDate(5)">5 Years</button> <button class="btn theme-dark-btn" style="color: white" id="10Year" onclick="changeDate(10)">10 Years</button> <button class="btn theme-dark-btn" style="color: white" id="20Year" onclick="changeDate(20)">20 Years</button> </div></pre> </p>
P粉315680565
P粉315680565

reply all(1)
P粉020556231

First of all, when you click the button, you already give it focus, you don't need to dynamically give it focus.

So, why doesn’t the background color change?

Because background-image has already covered background-color

You only need to set background instead of background-color

.btn:focus {
        background: #1ACC8D;
    }

Complete example, no need JS

.theme-dark-btn {
  transition: all ease 1s;
  background-image: linear-gradient(to left, #1ACC8D, #1ACC8D, #235FCD, #1C4CA3);
  background-size: 300%;
  background-position: 0 0;
  border: 1px solid #1C4CA3;
  font-weight: 600;
  letter-spacing: 1px;
}

.theme-dark-btn:hover {
  background-position: 100% 0;
  border: 1px solid #1ACC8D;
}

.theme-dark-btn:focus {
  background-color: #1ACC8D;
}

.theme-dark-btn:active {
  background-color: #1ACC8D;
}

.btn {
  height: 38px;
  line-height: 35px;
  text-align: center;
  padding: 0 18px;
  text-transform: uppercase;
  transition: background-image .3s linear;
  transition: box-shadow .3s linear;
  -webkit-border-radius: 20px;
  -moz-border-radius: 20px;
  border-radius: 20px;
  font-size: 12px !important;
}

.btn:focus {
  background: #1ACC8D;
}
<div class="col">
  <button class="btn theme-dark-btn" type="button" style="color: white" id="1Year" autofocus>1 Year</button>
  <button class="btn theme-dark-btn" style="color: white" id="5Year">5 Years</button>
  <button class="btn theme-dark-btn" style="color: white" id="10Year">10 Years</button>
  <button class="btn theme-dark-btn" style="color: white" id="20Year">20 Years</button>
</div>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template