0

I've been working on this subject matter for like 4 hours and I really cannot figure out why is this happening ..

Here's what is happening.. I have a function to remove an element . then updates remaining element in sequence.

The first time a user remove it works fine, but the next time, does not.

Below are the code on how do I remove the element. Check the selectedCard and selectedCardContainer variable.

function _removeSelectedCard( selectedCard ) {
   selectedCard = $(selectedCard).closest(".btn-remove-video");
   var selectedCardContainer = _getElementData(selectedCard, "parent");
   $(selectedCardContainer).remove();
}

Findings is that selectedCardContainer returns a value that it's not supposed to be return. e.g., selectedCard data-attr value = "id-1" but jQuery.data() return "id-2" .

Why is that happening? where did he get that value?

Here's the Fiddle

JF-Mechs
  • 1,083
  • 10
  • 23
  • 1
    What's `_getElementData`? – lc. Jun 07 '16 at 02:09
  • I just box out jQuery.data() – JF-Mechs Jun 07 '16 at 02:09
  • 1
    Not sure what to say then, can you make a minimal, verifiable example (i.e. remove all code not *directly* related to this problem from your Fiddle)? – lc. Jun 07 '16 at 02:14
  • Change `_setElementDataAttribute()` to use `.data()` instead of `.attr()`. – Barmar Jun 07 '16 at 02:15
  • And remove the `data-` prefix on the attributes when you call it. – Barmar Jun 07 '16 at 02:16
  • @lc Pardon me, don't get what you you mean. make a minimal verifiable example . the sample I gave above is where I am having a problem I guess? that is why if you go into the sample fiddle I have console the 2 variable I've mention above. which is if you look into them you'll see what I mean – JF-Mechs Jun 07 '16 at 02:17
  • @Barmar actually that's the first method I used.. but It doesn't update the data-attribute . I don't know why – JF-Mechs Jun 07 '16 at 02:18
  • 1
    That's correct. jQuery caches data internally, it doesn't use the attribute except to get the initial value. So you have to be consistent: either use `.data()` for everything or use `.attr()` for everything, but don't mix them. – Barmar Jun 07 '16 at 02:21
  • Why do you need it to update the data-attribute? – Barmar Jun 07 '16 at 02:21
  • @Barmar thanks for the info man! and another thanks for pointing out the dup question – JF-Mechs Jun 07 '16 at 02:22
  • I need to update the data-attribute because I'm using it as a selector for removing the container – JF-Mechs Jun 07 '16 at 02:22
  • You could just use a generic class value for all your panels and .closest to grab the immediate parent of the button clicked. Something like $(selectedCard).closest(".video-card-container").remove(); – georaldc Jun 07 '16 at 02:24

0 Answers0