Robert Anderson
Robert Anderson

Reputation: 3

Toggle link/button color on new button click

I am trying to import some customized buttons into a marketing survey application. I would like to set up some buttons styled with CSS and Jquery with some specific functionality.

I need to have the buttons change appearance and stay on the click. I have been able to accomplish this using the code below.

I need help in creating a function that returns any previously selected buttons to the original state while changing the newly selected button to the "clicked" state.

Detailed help would be most appreciated!

HTML:

<body>
<a href="#" ID="button">Test Link 1</a><link href="newStyleSheet.css" rel="stylesheet" type="text/css" />

<a href="#" ID="button">Test Link 2</a><link href="newStyleSheet.css" rel="stylesheet" type="text/css" />

<script src="jquery.min.js" type="text/javascript"></script>


<script type="text/javascript">
  $(document).ready(function(){
     $('a').click(function(){
       $ (this).toggleClass('clicked');
     });
  });
</script>
</body>

CSS:

a#button{
    -moz-box-shadow:inset 0px 1px 0px 0px #ffffff;
    -webkit-box-shadow:inset 0px 1px 0px 0px #ffffff;
    box-shadow:inset 0px 1px 0px 0px #ffffff;
    background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #ededed), color-stop(1, #dfdfdf) );
    background:-moz-linear-gradient( center top, #ededed 5%, #dfdfdf 100% );
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ededed', endColorstr='#dfdfdf');
    background-color:#ededed;
    -moz-border-radius:6px;
    -webkit-border-radius:6px;
    border-radius:6px;
    border:1px solid #dcdcdc;
    display:inline-block;
    color:#777777;
    font-family:arial;
    font-size:15px;
    font-weight:bold;
    text-decoration:none;
    text-shadow:1px 1px 0px #ffffff;
    width: 160px;
    padding-top: 6px;
    padding-right: 24px;
    padding-bottom: 6px;
    padding-left: 24px;
    text-align: center;
    vertical-align: middle;
    margin-right: 30px;
    margin-left: 30px;
}
a.clicked#button{
    background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #e5c9ab), color-stop(1, #d95936) );
    background:-moz-linear-gradient( center top, #e5c9ab 5%, #d95936 100% );
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#e5c9ab', endColorstr='#d95936');
    background-color:#dfdfdf;
    text-shadow:1px 1px 0px #777777;
    color:#ffffff;
}

Upvotes: 0

Views: 758

Answers (2)

David Thomas
David Thomas

Reputation: 253318

First, you've got two elements with the same id; this is invalid HTML: an id must be unique within the document. If you want several elements to share the same styles, use a class-name instead. That said, I've amended your HTML to:

<a href="#" class="button">Test Link 1</a>
<a href="#" class="button">Test Link 2</a>

As for your jQuery I'd suggest:

$('a.button').click(function(e){
    e.preventDefault();
    $(this).addClass('clicked').siblings().removeClass('clicked');
});

JS Fiddle demo.

The above assumes the elements will always be siblings; if they may not be:

$('a.button').click(function(e){
    e.preventDefault();
    $('a.clicked').removeClass('clicked');
    $(this).addClass('clicked');
});

JS Fiddle demo.

References:

Upvotes: 1

Adil Shaikh
Adil Shaikh

Reputation: 44740

$(document).ready(function(){
     $('a').click(function(){
         // reset all buttons to un-clicked state
         $('a').removeClass('clicked');
         $(this).toggleClass('clicked');
     });
});

Upvotes: 0

Related Questions