[Solved] $(‘.selector’).closest(‘div’) for non click element


I assume you mean something like this?

<script type="text/javascript">
$(document).ready(function() {
   $("#MyLink").click(function() {
      alert($(this).closest('div').html());
   });
});
</script>

This requires you to add id to the link.

If you mean show the alert without clicking anything you’ll have to explain how exactly you want it to show, meaning in response to what event?

Edit: in case you have more than one element, use class instead e.g. <a class="MyLink" ...> then have such code:

$(document).ready(function() {
   $(".MyLink").click(function() {
      alert($(this).closest('div').html());
   });
});

Using . instead of # will give all elements with that class.

0

solved $(‘.selector’).closest(‘div’) for non click element