0

I am using jQuery for a panel to show and hide. There is a small arrow image on it which also changes on hide and show. My code is

<div class="trigger-m"><span id="toppanelarrow">&nbsp;</span></div>

Now a class is added to_up when the panel opens and to_down when the panel closed. I have respective up and down arrow image for this.

Can someone help me adding a jQuery on click event of 'toppanelarrow' so a new class gets added to the span.

I have tried $().addClass().removeClass() but this didn't work.

Update: Please find the fiddle implementation http://jsfiddle.net/B5QUj/4/

Thanks

2
  • show the code and HTML (in jsfiddle.net if possible) Commented Mar 14, 2012 at 14:07
  • Sorry Guys for replying late. I have coded it in jsfiddle and here is the link. jsfiddle.net/B5QUj/3 Commented Mar 14, 2012 at 18:28

5 Answers 5

3

If you have one class on the element to start with:

$('#foo').on('click', function () {
  $(this).toggleClass('to_up to_down');
});​

is all you need.

demo: http://jsfiddle.net/kXNk8/

Sign up to request clarification or add additional context in comments.

1 Comment

this is good fast and way to do it
2

Also, you can try

$(this).toggleClass('classname');

Comments

1

You should try (this adds the class to_down if the span has the class to_up

$('#toppanelarrow').click(function(){
   if($(this).hasClass('to_up')){
     $(this).addClass('to_down').removeClass('to_up');
   }else{
     $(this).addClass('to_up').removeClass('to_down');
   }
});

this could also be written

$('#toppanelarrow').click(function(){
      $(this).toggleClass('to_up to_down').;
});

Comments

0

To get the wanted effect:

Html:

<input id="btn" type="button" value="click it" />

<div class="box">
    <span>Tekst</span>
</div>

js:

$(document).ready(function(){

    $("#btn").click(function(){
        $(".box").toggle();
    });    
});

Comments

0

Try:

$("#yourClassID").addClass("cssClass");

Comments

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.