Methods to Selecting First Element by ClassName
Different methods to selecting first element by class
by vol7ron
HTML
<div class="hi">
<div class="hey">hello1</div> // this is 1
<div class="hey">hello2</div> // this is 2
<div class="hey">hello3</div> // this is 3
</div>
JavaScript
// Pure JavaScript
var els = document.getElementsByClassName('hey');
alert("JS: " + els[0].innerHTML); // hello1
// jQuery
alert("jQuery Method 1: " + $(".hey:eq(0)").html());
alert("jQuery Method 2: " + $(".hey:first").html());
alert("jQuery Method 3: " + $(".hey").eq(0).html());