I am in the process of designing an FAQ page. Each question has a plus sign next to it that can be clicked to reveal the answer. I have successfully used jQuery to add a class called 'active' when a question is clicked, but since there are multiple questions on the page, I would like to find a way to avoid repeating the same code for each one. I have managed to identify the parent ID, however, I am struggling with how to store it as a variable for reuse within the jQuery script.
Here is what I am aiming to achieve:
var element = $(this).parent().parent().attr('id')
$('.expand').click(function(){
$('element .expand').toggleClass('active')
})
Is there a solution to this issue? When I attempt to use the 'element' variable, I receive an 'undefined' response:
$('.expand').click(function(){
console.log(element)
});