Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i toggle the color of the text inside a button

I’m trying to toggle the color of the text inside a button after it’s been clicked, currently the background color toggles on and off when clicking on a button, but the text color of all buttons change at same time, i’d only like the selected button to change, how do i fix this ?

$(document).on('click', '.newsbutton', function() {
  $(this).toggleClass("active");
});
$(document).on('click', '.newsbutton', '.btn-2', function() {
  $('.btn-2').addClass('selected');
});
.selected {
  color: #56CCF2;
}

.active {
  background-color: #242424 !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="btn btn-secondary newsbutton">
            <p href="" class="btn-2">BBC</p> 
            </button>

<button class="btn btn-secondary newsbutton">
            <p class="btn-2">The Guardian</p> 
            </button>

<button class="btn btn-secondary newsbutton">
            <p class="btn-2">Financial Times</p> 
            </button>

<button class="btn btn-secondary newsbutton">
            <p class="btn-2">Metro</p> 
            </button>

<button class="btn btn-secondary newsbutton">
            <p class="btn-2">Techcrunch</p> 
            </button>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You cannot have p inside button. Buttons can only have phrasing content. Use spans instead.

That being said, you are explicitly telling jQuery

$('.btn-2').addClass('selected');

to add that class to all elements matching .btn-2. Instead, use this and find:

$(document).on('click', '.newsbutton', function() {
  $(this).toggleClass('active').find('.btn-2').addClass('selected');
});
.selected {
  color: #56CCF2;
}

.active {
  background-color: #242424 !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="btn btn-secondary newsbutton"><span class="btn-2">BBC</span></button>

<button class="btn btn-secondary newsbutton"><span class="btn-2">The Guardian</span></button>

<button class="btn btn-secondary newsbutton"><span class="btn-2">Financial Times</span></button>

<button class="btn btn-secondary newsbutton"><span class="btn-2">Metro</span></button>

<button class="btn btn-secondary newsbutton"><span class="btn-2">Techcrunch</span></button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading