Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9179097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:42:46+00:00 2026-06-17T17:42:46+00:00

Imagine a somehow complex HTML structure, you don’t know exactly the positions of things

  • 0

Imagine a somehow complex HTML structure, you don’t know exactly the positions of things because some elements move over time, others move with hover, etc etc. Whatever the reasons, you just don’t know their positions/margins/paddings.

My question is this: If I have a single container (#my_element_container), contained within some wrappers of which I don’t have that much info (#complex_wrapper), which holds a random amount of elements (#my_element_container li), I want to be able to attach a touchstart event listener on the container and be able to tell in which element the user clicked.

The styles of this example are as follow:

#complex_wrapper {
    position: absolute;
    width: 600px;
    height: 600px;
    margin-top: 50px;   /* These two change randomly so I can't hardcode a -50! */
    margin-left: 12px;
    background-color: gray;
}
#my_element_container {
    position: absolute;
    top: 95px;
    left: 31px;
}
#my_element_container li{
    position: relative;
    width: 480px;
    height: 90px;
    background-color: red;
    border: 1px black solid;
}

Someone hinted in a previous post that I had to somehow manage to substract the margins and paddings of the container, so I would get a 0 coordinate at the top of the element and I could calculate from that. I understand that logic, but what I’m having trouble finding is a way to make that “substracting value” dynamically, whatever the other margins are.

I made a jsfiddle to show what I am asking, my goal is to be able to, whatever I hardcode into #complex_wrapper, tell which element was clicked: http://jsfiddle.net/342jU/1/

Our project doesn’t and won’t use jquery (not sure of the reasons, I just know we don’t).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:42:47+00:00Added an answer on June 17, 2026 at 5:42 pm

    You could use .elementFromPoint(x,y), where x,y are obviously the mouse event coordinates (I never used touchstart, but I suppose you can get the coordinates in a similar way).
    Something like that: http://jsfiddle.net/Polmonite/Sty7d/ .

    It becomes like this:

    my_element_container.addEventListener('click', function(e) {
        e.preventDefault();
        var element = document.elementFromPoint(e.clientX,e.clientY);
    
    // do something with element
    
    }, false);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I start using an ontology because it provides some useful vocabulary to describe
imagine this html on a page <div id=hpl_content_wrap> <p class=foobar>this is one word and
Imagine you have two views with code like the following: controller_a/a.html.erb <%= content_tag(:div) do
Imagine a very big gwt project of application. This is just a some form
Imagine that I have a view with some UIKit object as its subview (for
Imagine the following scenario: some xml file is downloaded from server and stored on
Imagine I have a string - something like this: This is some really cool
Imagine you have a library for working with some sort of XML file or
The scenario Imagine I am forced to work with some of my files always
Is it possible to bind hotkeys to messagebox somehow? Imagine a scenario, where a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.