Find element with same class name (match class name)
This is probably a dumb question (noob) but I cannot seem to find the answer, either here or on StackOverflow or even Googling...
All I want to do is, when clicking on an element, find any other elements with the same class name (and show/hide or do something with them).
This is as far as I got:
- $('option').click(function() {
// get the class name of the option element
var className = $(this).attr("class");
// find the div with that same class name and show it
$('div').hasClass(className).show();
});
(In this case I'm using a drop-down menu and a div.)
It works fine when a specific class name is used, e.g.:
- $('option.foo').click(function() {
$('div.foo').show();
});
But I need the class name to be a variable so this function is reusable.
What am I doing wrong?
Any help is greatly appreciated, thank you.