Eyal Eyal - 7 days ago 4
jQuery Question

Find the first top nearest <h3> element

I need to select the

<h3>
element from the inner checkbox. I couldn't find any
jquery
selector to do it. how can I do this?

<h3 id="row_3">Date</h3>
<!-- 2 second-->
<div class='container'>
<p>
<input type="checkbox" checked class="mathOperator" />
</p>
</div>

Answer

You need to do a combination of .closest() and .prev():

$(this).closest(".container").prev("h3");   // h3

Not sure why you wanna do when you have an id:

$("#row_3");                                // h3

Snippet

$(function () {
  $(".mathOperator").click(function () {
    console.log($(this).closest(".container").prev("h3").text());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3 id="row_3">Date</h3>
<!-- 2 second-->
<div class='container'>
  <p>  
    <input type="checkbox" checked class="mathOperator" />              
  </p>
</div>

Just on a side note, never duplicate the ids.