0

I have a parent div named #container and have many children which have a common class of .timescalebase. My requirement is when I click in my parent div I want the nearest child. I can't use the child id for click event because its width is 0px.

<div id="container" style="width: 70%; margin-top: 15px; position: absolute;">
    <div class="timescalebase" id="1"></div>
    <div class="timescalebase" id="2"></div>
    <div class="timescalebase" id="3"></div>
</div>
 $(document).on('click', '#container', function (e) {
    base = $(this).closest(".timescalebase")
    baseid = base.attr('id');
});
11
  • In such a case, you can't do it. Commented Jan 3, 2017 at 11:50
  • What do you mean by the 'closest child'? Can you not just put the click event handler directly on the #container .timescalebase elements and use this within the handler to refer to the element? Commented Jan 3, 2017 at 11:51
  • then, is there any suggestion please
    – manu p
    Commented Jan 3, 2017 at 11:51
  • 1
    id=1; is invalid HTML.
    – connexo
    Commented Jan 3, 2017 at 11:51
  • 4
    How are the child divs positioned? A working example of the HTML (including any CSS) would help a lot here. Commented Jan 3, 2017 at 11:53

5 Answers 5

1

May be this is the solution;

var ruler;
var nearestDiv;
$("#container").on("click",function (e) {
  ruler = 10000000;
  $(".timescalebase").each(function(i, a) {
    if (Math.abs(e.pageY - $(a).offset().top) < ruler) {
      ruler = Math.abs(e.pageY - $(a).offset().top);
      nearestDiv = a;
    }
  });

  var baseid = $(nearestDiv).attr("id");
  alert(baseid);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container" style="width: 70%; margin-top: 15px;">
  <div class="timescalebase" id="a1">A....</div>
  <br/>
  <br/>
  <div class="timescalebase" id="a2">B....</div>
  <br/>
  <br/>
  <div class="timescalebase" id="a3">C....</div>
  <br/>
  <br/>
</div>

0
1

Use jQuery offset() method to find the position of all the child elements and compare to pageX and pageY of the mouse click event.

http://api.jquery.com/offset/

http://api.jquery.com/event.pageX/

http://api.jquery.com/event.pageY/

0

Is this what you want?

$(document).on('click', '#container', function (e) {
    var closestChild = $(this).find(".timescalebase:first");
});
7
  • 1
    No... I guess. The OP is looking for ... Read it again. Commented Jan 3, 2017 at 12:02
  • What is meant by nearest child?
    – Hemal
    Commented Jan 3, 2017 at 12:03
  • 1
    When you click on that random div, some div should be near your clicked area right? Looks like this question is crazy and unanswerable. Commented Jan 3, 2017 at 12:04
  • 1
    By the way all child are nearest to the parent. hahahah
    – Hemal
    Commented Jan 3, 2017 at 12:05
  • 1
    Yes, it is better you delete this answer till you get a clarity. See the comments in the question. Esp by Rory. Commented Jan 3, 2017 at 12:05
0

Closest from JQuery :For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.

Closest will look for closet parent not child, you need to find child.

Working fiddle to get first child;

<div id="container" style="width: 70%; margin-top: 15px; position: absolute;">
click on me
    <div class="timescalebase" id='1'></div>
    <div class="timescalebase" id='2'></div>
    <div class="timescalebase" id='3'></div>
</div>
 $(document).on('click', '#container', function (e) {
    var base = $(this).find(".timescalebase").first();
    var baseid = base.attr('id');
    alert(baseid);
});

closest() selects the first element that matches the selector, up from the DOM tree, you will be getting the container div itself.

-1

In such case you can use code below :

   $("#container").on("click", function ()
     {
      //if required first div
    alert($(this).find(".timescalebase:eq(0)").attr("id"));
      //if required second div
    alert($(this).find(".timescalebase:eq(1)").attr("id"));
      //if required third div
    alert($(this).find(".timescalebase:eq(2)").attr("id"));
    });

Not the answer you're looking for? Browse other questions tagged or ask your own question.