0

I need a help regarding a click function.

In starting I need to show few class with different div classname inside a main div class which can be ID. Now on click of a anchor I will grab that classname and leaving that all other class inside that main div need to be visible. All classes will start with a unique name like marker_automobile, marker_sample-class, etc.

Now on click of marker_automobile, the marker_sample-class, etc need to be hide and only marker_automobile need to be shown. Now on click of marker_sample-class, all other classes expect this should be hide.

Sorry for my bad english.

Link to jsfiddle

<a href="#" class="changer" data="data1">Data1</a>
<a href="#" class="changer" data="data2">Data2</a>
<a href="#" class="changer" data="data3">Data3</a>

<div id="content">
  <div class="data1">
    content 1
  </div>
  <div class="data1">
    content 2
  </div>
  <div class="data2">
    content 3
  </div>
  <div class="data3">
    content 4
  </div>
  <div class="data1">
    content 5
  </div>
</div>


$(document).ready(function(){

   $(".changer").click(function() {
       var data = $(this).attr("data");
       $("."+data).css( "display", "none" );
    });

});
1
  • That's not the most elegant solution, but it worked here. Or I didn't understand what you actually want. Commented May 26, 2014 at 17:46

2 Answers 2

2

You should make all div elements visible again when button is clicked. Try this:

 $(document).ready(function(){ 
   $(".changer").click(function() {
       $("div[class^=data]").show(); //show all divs
       var data = $(this).attr("data");
       $("div[class^=data]").not("."+data).css("display","none");//hide button 
       related ones
    });
});

DEMO

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

3 Comments

HI Actually what I need to do is to hide the other classes and show the one which is clicked. :(
Yes, worked. Can you tell me a way in which I can again show all the class again on click of ALL.
Just use this $("div[class^=data]").show(); to show all.
2

The issue is because you are not making all div elements visible again when the button is clicked for consecutive times. Also, data is not a valid attribute. In my example I changed your HTML to use a data-* attribute. Try this:

<a href="#" class="changer" data-rel="data1">Data1</a>
<a href="#" class="changer" data-rel="data2">Data2</a>
<a href="#" class="changer" data-rel="data3">Data3</a>
$(".changer").click(function () {
    $('#content div').show(); // show all elements again
    $("." + $(this).data('rel')).hide(); // hide the related ones only
});

Updated fiddle

3 Comments

HI Actually what I need to do is to hide the other classes and show the one which is clicked. :(
In that case just swap over hide() and show() in my above example.
Yes, worked. Can you tell me a way in which I can again show all the class again on click of ALL.

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.