Easy jQuery question - outputting a div with the same class as a
I want to do this, but I have not yet realized that it is completely ...
$(document).ready(function() {
$("a.whateverclass").click(function() {
$("div.whateverclass").show();
return false;
});
Basically when clicking a link with a specific class, all divs with that class are displayed. Classes can be any class. And I won't know the name (s) of the classes in the application.js file, so I need to match equal classes.
0
source to share
2 answers
I like @ Eran's answer, but in case you have links that don't match this pattern, you might want to make sure you only apply this to links that do.
$('a[class]').click(function() {
$('div.' + $(this).attr('class')).show();
return false;
});
And in the case where references may have other classes to them, you can use a naming scheme such as:
$('a[class^=div-]').click(function() {
$('div.' + $(this).attr('class')).show();
return false;
});
<div class='div-mydiv'></div>
<a href='javascript:void(0);' class='div-mydiv'>Show</a>
<a href='mailto:nobody@nowhere.com' class='mail-link'>Contact Us</a>
+4
source to share