1
I want to make a code that takes the class of an ID whenever it is clicked:
html
<ul>
<li id="alert-class" class="class-1">1</li>
<li id="alert-class" class="class-2">2</li>
<li id="alert-class" class="class-3">3</li>
</ul>
Javascript
<script type="text/javascript">
var $j = jQuery.noConflict();
$j(document).ready(function() {
var $alertando = '';
$j("#alert-class").click(function() {
$alertando = $j(this).attr("class");
alert($alertando);
});
});
</script>
Until it works well, the alert prints the right information, but only prints the value of the first "read" when I click on it. The other "li" s and their respective class alerts do not work.
Well, I tried to do it in several different ways, tried to use "each" but failed to simplify my code.
Where I want to use it is much more complex than that, but I found it better to simplify and facilitate understanding and ends up working the same way, I imagine.
I await answers and thank you in advance!
Simply why a id, should be unique, then the selector finds the first and for, you are used id for the wrong purposes. Review your structure.
– Fernando Leal
Actually, from what I understand, what are you using on
class
, should beid
and vice versa...– Felipe Avelar