JavaScript Show and Hide Div With Matching Class Name On Click show hide with animation,show hide with css,show hide

I am still getting to grips with jQuery and wondered if anyone had a suggestion for this?

Basically on click I want to show the div with the matching class, so if you click the btn with class '.project1' then it should show the content div with the same class of '.project1'.

I'm just stuck on how it would find this so any suggestions would be awesome :)

Thanks in advance.

Snippet:

$('div[class*="project"]').click(function (e) {
	$(this).closest('.content').show();
});
.content {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="project1 btn">BTN 1</div>
<div class="project2 btn">BTN 2</div>
<div class="project3 btn">BTN 3</div>
<div class="project4 btn">BTN 4</div>


<div class="project1 content">CONTENT 1</div>
<div class="project2 content">CONTENT 2</div>
<div class="project3 content">CONTENT 3</div>
<div class="project4 content">CONTENT 4</div>
Answer:1

One option is to use data() to store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.

$('div[class*="project"]').click(function (e) {
    var project = $(this).data("project");        //Get the data-project of the clicked element
    $(".content.project" + project).toggle();     //Toggle the element with content and project class
});
.content {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="project1 btn" data-project="1">BTN 1</div>   <!-- Add data-project on HTML-->
<div class="project2 btn" data-project="2">BTN 2</div>
<div class="project3 btn" data-project="3">BTN 3</div>
<div class="project4 btn" data-project="4">BTN 4</div>


<div class="project1 content" data-project="1">CONTENT 1</div>
<div class="project2 content" data-project="2">CONTENT 2</div>
<div class="project3 content" data-project="3">CONTENT 3</div>
<div class="project4 content" data-project="4">CONTENT 4</div>
Answer:2

You need to find the project number you clicked on.

You can do that by filtering the classes of the button you clicked on and extracting the number :

+[...this.classList].find(c => c.startsWith('project')).replace('project', '');

Then all you have to do is toggle the targeted project content :

$('.content.project'+project).toggle();

Here's a working example:

$('div[class^="project"]').on('click', function(){
  const project = +[...this.classList].find(c => c.startsWith('project')).replace('project', '');
  
  $('.content.project'+project).toggle();
});
.content {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="project1 btn">BTN 1</div>
<div class="project2 btn">BTN 2</div>
<div class="project3 btn">BTN 3</div>
<div class="project4 btn">BTN 4</div>


<div class="project1 content">CONTENT 1</div>
<div class="project2 content">CONTENT 2</div>
<div class="project3 content">CONTENT 3</div>
<div class="project4 content">CONTENT 4</div>
Answer:3

Toggling based on class matching is one of the harder thing to do in JQuery, however to achieve what you require you could do the following:

/* When a btn is clicked */
$('.btn').click(function(e) {

  /* Extract the classes of this button element */
  const classes = $(this).attr('class');

  /* Parse the project class part of the classes string */
  const projectClass = classes.match(/project\d+/)[0];

  /* Construct a matcher for the projects corresponding
  content and "toggle" the content's visiblity */
  $('.content.' + projectClass).toggle();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<div class="project1 btn">BTN 1</div>
<div class="project2 btn">BTN 2</div>
<div class="project3 btn">BTN 3</div>
<div class="project4 btn">BTN 4</div>


<div class="project1 content">CONTENT 1</div>
<div class="project2 content">CONTENT 2</div>
<div class="project3 content">CONTENT 3</div>
<div class="project4 content">CONTENT 4</div>
Answer:4

I'm quite new in Node.js (and Javascript in general) programming and I have a huge doubt. I would like to restyle some code I've written in order to fit the OOP style, so I need to define some classes:...

I'm quite new in Node.js (and Javascript in general) programming and I have a huge doubt. I would like to restyle some code I've written in order to fit the OOP style, so I need to define some classes:...

  1. javascript class definition example
  2. javascript class definition best practice
  3. javascript class definition
  4. javascript class definition prototype
  5. javascript class definition syntax
  6. javascript class definition internet explorer
  7. javascript class definition es6
  8. javascript class definition constructor
  9. javascript class definition property
  10. javascript export class definition
  11. javascript class method definition
  12. javascript class function definition
  13. javascript change class definition
  14. javascript class variable definition
  15. javascript dynamic class definition
  16. javascript get class definition
  17. javascript delete class definition
  18. javascript add class definition
  19. javascript object class definition
  20. javascript import class definition

I have create come component in reactJs, consist of 2 button which handle each onClick function, But when I run the code "myfunction is not defined" error is appear. I want to redirect page to ...

I have create come component in reactJs, consist of 2 button which handle each onClick function, But when I run the code "myfunction is not defined" error is appear. I want to redirect page to ...

I would like to volume mount a directory from a Docker container to my work station, so when I edit the content in the volume mount from my work station it updated in the container as well. It would ...

I would like to volume mount a directory from a Docker container to my work station, so when I edit the content in the volume mount from my work station it updated in the container as well. It would ...

So, the parent class Select declares this.elem as a DOM-element <select> and this.value, that links to a value of selected option class Select { constructor(classList, isTwoLevel, index){ ...

So, the parent class Select declares this.elem as a DOM-element <select> and this.value, that links to a value of selected option class Select { constructor(classList, isTwoLevel, index){ ...