10

Im trying to get the position of same object in different places, where ,with a javascript function, I should get different top positions but thats not the scenario. The script code:

<script type="text/javascript">
    window.ShowAlert = function myFunction(element) {   
    console.log("Hello World.");
    alert(element.offsetTop);
   }
</script>

The Index.razor code:

@inject IJSRuntime jsRuntime

<div>
    @for (int i = 0; i < 10; i++)
    {
        <div @onclick="MemberFunction" @ref="memberRef">Click Here</div>
    }

</div>

@code {

    private ElementReference memberRef;
    void MemberFunction()
    {
        jsRuntime.InvokeAsync<object>("ShowAlert", memberRef);
    }
}

As you can see here Im doing a for in the same div, where he goes down the line. What I want from this is for every div posted it should give me a different value of offsetTop, because he goes down the line one by one. How can I manage this problem?

For a better understanding here you have a demo https://blazorfiddle.com/s/4g57o82k . As you can see in the demo the value for each Click Here is the same.

Thank you for your attention.

Vencovsky
  • 28,550
  • 17
  • 109
  • 176
LOL Jovem
  • 197
  • 2
  • 17
  • Possible duplicate of [Get position/offset of element relative to a parent container?](https://stackoverflow.com/questions/11634770/get-position-offset-of-element-relative-to-a-parent-container) – ControlAltDel Oct 02 '19 at 15:59
  • same element is passed in memberRef no matter on which div you are clicking – Babar Hussain Oct 02 '19 at 16:00
  • @ControlAltDel let me have a check. – LOL Jovem Oct 02 '19 at 16:00
  • @BabarBilal what you suggest to do? – LOL Jovem Oct 02 '19 at 16:01
  • @ControlAltDel I don't think so, I having an issue with the same value for same object in different positions, not having with getting the element position relative to a parent. – LOL Jovem Oct 02 '19 at 16:04
  • 2
    I happened across this question and found a way to pass the index: https://blazorfiddle.com/s/50nwz2is –  Oct 02 '19 at 16:34
  • @LOLJovem I opened an [issue](https://github.com/aspnet/AspNetCore/issues/14700) that references this question. – Vencovsky Oct 03 '19 at 13:16

1 Answers1

13

Edit:

I believe that this is a bug, so I created an issue. If it's not an issue or it get fixed, I will update here.

Looking at the docs about lambda expressions

Do not use the loop variable (i) in a for loop directly in a lambda expression. Otherwise the same variable is used by all lambda expressions causing i's value to be the same in all lambdas. Always capture its value in a local variable (buttonNumber in the preceding example) and then use it.

So looking at the issue and the answer it was given I tweak it a little and manage to make it work.

You need to define a new varible inside the for to hold the value of i.

@inject IJSRuntime jsRuntime

<div>
    @for(var i = 0; i < memberRef.Count(); i++)
    {
        var i2 = i;
        <div @onclick="() => MemberFunction(i2)" @ref="memberRef[i2]">Click Here</div>
    }
</div>

@code {

    private ElementReference[] memberRef { get; set; } = new ElementReference[11];
    void MemberFunction(int i)
    {
        jsRuntime.InvokeAsync<object>("ShowAlert", memberRef[i]);
    }

}

Here is a working fiddle.

Vencovsky
  • 28,550
  • 17
  • 109
  • 176
  • For some reason Im not able to test, some errors occurring in this line:`
    MemberFunction(i)" @ref="memberRef[i]">`
    – LOL Jovem Oct 02 '19 at 16:40
  • @LOLJovem the error is that it doesnt find `i`. I'm trying to solve that too, just a sec – Vencovsky Oct 02 '19 at 16:42
  • Ok no problem. Go ahead! – LOL Jovem Oct 02 '19 at 16:44
  • @LOLJovem boy... this is going to be hard. I edited the asnwer and the code works, but it it also only sends the last element of the array. I'm trying to find out why but no clue so far – Vencovsky Oct 02 '19 at 17:15
  • I believe and understand, I appreciate all the work that you are doing. And im not worry about the time or anything that could go wrong. Have your time! Good luck! – LOL Jovem Oct 02 '19 at 17:19
  • Thank you one more time! Appreciate all the work you did here! Thank you so much! – LOL Jovem Oct 04 '19 at 13:06