Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

jQuery Click then 2nd Click

Featured Replies

Hello Friends

 

JSFiddle have made things so much easier ! Here's a link. :)

 

What I want to achieve is an alternation between two states. So user clicks once, the box goes Yellow, the user clicks a second time, the box goes back to Red, and the effect to repeat.

 

I don't know how to do that and I would greatly appreciate some help.

 

Thank You.

Edited by Ciwan


<style type="text/css">
.red { background-color: red; }
.yellow { background-color: yellow; }
</style>

<script type="text/javascript">
$(document).ready(function(){
$('#click').click(function(){
 if($(this).hasClass('red')) {
  $(this).removeClass('red').addClass('yellow');
 } else {
  $(this).removeClass('yellow').addClass('red');
 }
});
});
</script>

<div id="click" class="red"></div>

  • Author

Huh soo simple ! :)

 

Thanks Skidz. Is there a way to do it without adding the extra CSS ?

 

Thank You.

There is but tbh it would be more messy than this. Surely adding a couple of css classes to your stylesheet isn't that much of an issue?

  • Author

No It isn't an issue at all, I was just wondering if the other way was neater .. I care about neat code.

 

But you're saying it'll be more mess .. in which case leave it. :)

 

Thanks once again.

I always advocate that using .css() is terrible! You should always create style rules in the stylesheet not via javascript.

 

Real world scenario:

 

Say I come to your project in 6 months time because you've asked me to update something. I need to change the colour of the box. I would immediately be looking for style rules so I'd instantly go to the stylesheet.

Nice.

 

The only comment is to use some sort of identifier an your anchor tag (<a>) as your current selector will match any anchor on the page.

  • Author

Yep will do. This was just a JSFiddle for you guys to help me.

 

On the real site, I'll have to be much more specific with the selectors. :friends:

 

Thanks for all the help.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.