I'd like to get location information of<div id ="year-age">using jQuery.
Using the code below,

Uncaught TypeError: Cannot read property'top'of undefined

I get an error with

The jquery used is jquery-3.2.1.min.js.

for (var i = 1850;i&lt;2001;i ++) {
        var yearHeight = $('# year-' + i) .position (). top;

How can I get it?

  • Answer # 1

    Cannot read property'top'of undefined
    $('# year-'+ i) .position ()
    This is alreadyundefined.

    The possible reason is that$('# year-'+ i)cannot find the element you are trying to get.

    I'm trying to get

    iby 1 from 1850 to 2000 to get an element, but I really want to getyear-1850toyear-2000 Is there an element for all years without missing any?

  • Answer # 2

    <div id ="if year-1850">really exists

    var i = 1850;
    var yearHeight = $('# year-' + i) .position (). top;

    Isn't it possible to get this?
    Should I present other codes?