I’m sure this will be obvious to most of you. But I couldn’t figure it out for I’m a beginner with JS and jQuery.
I’ve got a lot of repetitive functions all targeting specific elements (the same way, though). Let’s say it’s a really simple function. We’re adding/toggling the class .add to an element #there by clicking the element #here. This goes on with another group of elements #here2 and #there2 and so on.
$(document).ready(function(){
$("#here").click(function(){
$("#there").toggleClass("add");
});
$("#here2").click(function(){
$("#there2").toggleClass("add");
});
$("#here3").click(function(){
$("#there3").toggleClass("add");
});
/* and so on */
});
Is there a simple way to achive what I’m looking for? Thank you. 🙂
I was trying to use a variable but couldn’t figure out how to define it in this case. Nor did I manage to create a “meta-function” that replaces all the possible functions. :/
elDoc74 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5