1

So I have a link, that when clicked, i want its css values to change. I can do that with this jquery code:

    $(".linkname").click(function(){
  $(this).css({'border-style' : 'solid', 'border-width' : '1px', 'border-color' : '#ec6163', 'color' : '#ec6163'});
  return false;
});

However, I also want the link's css values to revert to normal when I click the link with the altered css values a second time. I tried an if else sentence, but it always evaluated else and didn't work. How can i do this?

2
  • 2
    Please post up your version with the if statement. Commented Feb 19, 2011 at 8:20
  • 1
    Welcome to the community; as this is your first question, I'm going to point you to the FAQ. Also, it's worth mentioning now that you should select the checkmark next to the answer that best answers your question. Commented Feb 19, 2011 at 9:16

3 Answers 3

5

Even better, put all of that CSS into a class and use toggleClass to switch.

/* CSS */
.someclass { border: 1px solid #ec6163; color: #ec6163; }

/* JS */
$(".linkname").click(function(){
    $(this).toggleClass('someclass');
    return false;
});
Sign up to request clarification or add additional context in comments.

1 Comment

I think it depends on the situation, or rather the amount of use within the site. Toggling an input button on a contact page (one JS function). Toggling links, divs, etc. site-wide toggleClass/commented CSS - IMHO
3

Have you tried using toggle()?

$('.linkname').toggle( function() {
        $(this).css({'border-style' : 'solid', 'border-width' : '1px', 'border-color' : '#ec6163', 'color' : '#ec6163'});
    }, function() {
        $(this).css({'border-style' : 'dotted', 'border-width' : '0px', 'border-color' : '#999999', 'color' : '#cc6163'});
});

4 Comments

wow. thanks a ton. you just saved me a couple of hours. tytytyty
No problem. Had a similar issue myself recently.
You can use this.style.borderColor = instead of having to call $ to use the css() function
@Yi - You're right, but I was just trying to show the toggle function. With the amount of styles he's changing, I'd still call the css() function. (Answer edited to show all CSS changes in one line)
1

I am not sure why your if/else statement didn't work.
Perhaps try using a variable outside of the method to save which state the link is in.

eg.

var link_state = 1;

$(".linkname").click(function(){
  if (link_state == 1) {
    $(this).css({'border-style' : 'solid', 'border-width' : '1px', 'border-color' : '#ec6163', 'color' : '#ec6163'});
    link_state = 0;
  }
  else {
    $(this).css({'border-style' : 'none', 'border-width' : '0px', 'border-color' : '#ffffff', 'color' : '#ffffff'});
    link_state = 1;
  }
  return false;
});

3 Comments

You forgot the variable's underscore in the IF.
@D_N : It appear to be there for me, unless I misunderstand.
Hm, right you are. My mobile browser, Opera Mini, won't display it, for whatever dumb, quirky reason. Sorry for the false alarm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.