change class of element, on each mouse click - javascript

I want to change class of element, everytime I click on it.
on css section I have:
myButton.btn_first_look {
/* some atributes */
}
myButton.btn_second_look{
/* some atributes */
}
little jquery:
$(document).ready(function(){
$('#myButton').click(function() {
$(this).toggleClass('btn_first_look');
$(this).toggleClass('btn_second_look');
})};
So, when I click on element, I expect jquery script to change element class from btn_first_look to btn_second_look and otherwise.
But boom. Nothing happend

toggleClass will take care of add and remove by itself
$('#myButton').click(function() {
$(this).toggleClass('btn_second_look btn_first_look');
};
Demo Fiddle

Use removeClass() and addClass() respectively
$('#myButton').click(function() {
//if button has class btn_first_look
if($(this).hasClass('btn_first_look')){
$(this).removeClass('btn_first_look');
$(this).addClass('btn_second_look');
}
//if button has class btn_second_look
else if($(this).hasClass('btn_second_look')){
$(this).removeClass('btn_second_look');
$(this).addClass('btn_first_look');
}
});
This adds whichever class the button doesn't have, and removes the class it does.

Try this.
If your element has class btn_first_look from the start, you can write
$(element).toggleClass("btn_first_look btn_second_look");
This will remove class btn_first_look and add class btn_second_look. If you do that again, it will remove class btn_second_look and reinstate class btn_first_look .

Add a parenthesis at the very end. Your first (function() has no close, so it won't work.

That will work for you
$(document).ready(function(){
$('#myButton').click(function(){
$(this).toggleClass('btn_first_look btn_second_look');
});
});

Related

Add class to element when second div has class

I have a Li element (dropdown) that gets an Active class when parent div (button) is clicked. When this element have this class I want to give another div the same class. When the li element (dropdown) is clicked again the active class is removed, I then want to remove the active class on the second div aswell.
What I got so far:
$('document').ready(function() {
if ($('li').hasClass('active')) {
$("#site-overlay").addClass("active");
}
});
This works a bit on the way in console- it gives my second div the correct class. It doesnt work live though, I guess I cant just call it on pageload? It also doesnt remove the class.
$('li').on('click', function(){
if($(this).hasClass('active')) {
$("#site-overlay").addClass("active");
} else {
$("#site-overlay").removeClass("active");
}
});
Your current example is not setting the active class on the LI elements, so nothing happens, but it should work something like this:
$(function () {
$('ul.megamenu > li').on('click', function () {
$("#site-overlay").toggleClass("active", $(this).hasClass('active'));
});
});
toggleClass can take a second boolean parameter that controls turning on/off the class based on a true/false value.
Note: I hacked the following JSFiddle just to show something working (the clicking just toggles the site-overlay state for now):
https://jsfiddle.net/TrueBlueAussie/doxdmxmL/16/

toggling class Jquery

I have created a play and pause button for my video, I have trying to switch class when the button is pressed, I.e when you click on the pause button the class switches to and vice versa.
Below is a snippet of my code of and the link to the code is also here
$('#play-pause-btn').click(function() {
$('#video-wall__content').get(0).paused ?
$('#video-wall__content').get(0).play() : $('#video-wall__content').get(0).pause();
});
does anyone know how this can be done?
Check out removeClass(), and addClass()...Well they do what they are named after.
http://api.jquery.com/removeclass/
http://api.jquery.com/addclass/
If you want to change the class. Try .attr()
$("#td_id").attr('class', 'newClass');
Or .toggleClass():
$("#td_id").toggleClass('change_me newClass');
You can remove/add the classes on click using removeClass and addClass.
$('#play-pause-btn').click(function(){
if ($('#video-wall__content').get(0).paused) {
$('#video-wall__content').get(0).play();
// now playing
$(this).removeClass("paused").addClass("playing");
}
else {
$('#video-wall__content').get(0).pause();
// now paused
$(this).removeClass("playing").addClass("paused");
}
});
Addding the class when the page is opened
If you have access to the HTML, just add it to your tag:
<div id="play-pause-btn" class="playing> [...] </div>
If you can use only JavaScript:
$(document).ready(function() {
$("#play-pause-btn").addClass("playing");
});

Remove previously added class by jQuery

I have a follow up question to this: Add and remove a class on click using jQuery?
I'm able to add a class to a specific li element using this snipped by Jamie Taylor:
$('.team').on('click', 'li', function() {
$('.team li#member').removeClass('more');
$(this).addClass('more');
});
I'd like to remove the Class .more again, by clicking a toggle element inside the li-item.
My jQuery Snippet which doesn't work:
$('.toggle-x').click(function () {
$('.team li#member').removeClass('more');
})
Thanks for the help!
In your code, it will remove the class form only one element with id member. If you want to remove the class from all the li elements, use like this,
$('.team li.more').removeClass('more');
try this
$(".innerLiContent").click(function(){
$(this).parentsUntil( "li" ).removeClass('more');
});
Your problem is because when you click on an element X inside the <li /> the element fires the click event, and if you don't use event.stopPropagation() the <li /> will also fire the click event and the class will be added again.
Try this:
$('.toggle-x').click(function (e) {
$('.team li#member').removeClass('more');
e.stopPropagation();
})
It should work if that's your problem.

jQuery show hide and active class in one button

I have this code and I want to add active class to button .loginhead (to remove and add class when click and when content show and hide). How I can do that?
$(".top .login").hide('', function () {
$('.loginhead').removeClass("active");
});
$(".loginhead").show();
$('.loginhead').click(function () {
$(".top .login").slideToggle();
$('.loginhead').addClass("active");
});
use toggleClass(). If I'm understanding you right. Here's a link to documentation: http://api.jquery.com/toggleClass/
$('.loginhead').toggleClass("active");

change div class onclick on another div, and change back on body click

Let me define the problem a little bit more:
i have
<div class="contact">
<div id="form"></div>
<div id="icon"></div>
</div>
i want onclick on #icon, to change the class of .contact to .contactexpand( or just append it).
Then i want that the on body click to change the class back, but of course that shouldnt happen when clicking on the new class .contactexpand, and if possible that clicking on icon again changes the class back again.
I tried numerous examples and combinations but just couldn't get the right result and behavior.
Check this: Working example
Let's go step by step
I want onclick on #icon, to change the class of .contact to .contactexpand( or just append it). […] and if possible that clicking on icon again changes the class back again.
You want to use the toggleClass() method to achieve this. Simply:
$('#icon').on('click', function(e){
$(this).parent()
.toggleClass('contact')
.toggleClass('contactexpand');
});
Then i want that the on body click to change the class back
You will have to make sure that body removes contactexpand class and adds contact. At this point I would just give the container element an id (or class if you prefer), just to make things simpler. Then what you do is pretty simple:
$('body').on('click', function(e){
$('#thisdiv')
.removeClass('contactexpand')
.addClass('contact');
});
but of course that shouldnt happen when clicking on the new class .contactexpand.
This is the step that the other answers missed, I think. Since everywhere you click, you also click on the body element, you will always trigger the click event on the body, hence removing the contactexpand class and adding the contact one.
Enter event.stopPropagation(). This method will make sure that the events doesn't bubble up the DOM, and will not trigger the body click.
$('#thisdiv').on('click', function(e){
e.stopPropagation();
});
Working example
You can add a class to parent element like the following code.
$(".contact #icon").click(function(){
var element = $(this).parent(".contact");
element.removeClass("contact").addClass("contactexpand");
});
I like to the jQuerys toggleClass function like so:
$('#icon').click(function(){
$('#contactbox').toggleClass('contact');
$('#contactbox').toggleClass('contactexpand');
});
Or you could use addClass('className') and removerClass('className') if you would like to apend it rather than toggle it :)
Here is an example: http://jsfiddle.net/aUUkL/
You can also add an onclick event to the body of the page and use hasClass('className') to see whether or not to toggle the class when the body is clicked. You could use something like this (Although I havent tested this bit!):
$('body').click(function(){
if( $('#contactbox').hasClass('contactexpand') ){
$('#contactbox').addClass('contact');
$('#contactbox').removeClass('contactexpand');
}
});
You can do this
$('body').on('click', function(event) {
if ($(event.target).attr('id') == 'icon') {
$(event.target).parent().toggleClass('contactexpand');
} else {
$('.contact').removeClass('contactexpand');
}
});
Check out this jsfiddle
var $contact = $('.contact');
$contact.find('#icon').click(function(e, hide) {
e.stopPropagation();
$contact[hide ? 'removeClass' : 'toggleClass']('contactexpand');
});
$(document).on('click', function(e) {
if (e.srcElement === $contact[0]) return;
$contact.find('#icon').trigger('click', true);
});
http://jsfiddle.net/kZkuH/2/

Categories

Resources