-2

What i know is if I have

 <li class="media_item upload" data-actions-permissions="{}" data-info="{}">
 </li>

I can use $(".media_item.upload").data("info")

But it doesn't work when I have multiple li(s) and I need to get all the data-info for each item.

I was hoping something like $(".media_item.upload")[i].data("info") would work but it didn't.

T J
  • 42,762
  • 13
  • 83
  • 138
Yes
  • 63
  • 5
  • 2
    You seem to have answered your own question already...? – Rory McCrossan Jul 19 '16 at 20:04
  • 1
    Then use the same ? What the difference if earlier example ? – Rayon Jul 19 '16 at 20:04
  • 1
    `.attr("data-info")` – theblindprophet Jul 19 '16 at 20:05
  • 5
    You're using Angular and mixing it with jquery selectors...why? If this is the case, please don't, and read this: http://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background – Darren Wainwright Jul 19 '16 at 20:05
  • @theblindprophet you should always use `data()` where possible. It's quicker as it saves DOM accesses, and also coerces types for you instead of always returning a string. – Rory McCrossan Jul 19 '16 at 20:05
  • `$(".media_item.upload")[0].data("info")` Why the `[0]`? If you want the first one, use the correct jquery method to do so. – Kevin B Jul 19 '16 at 21:31
  • @KevinB, I want all of them.. so i thought `console.log($("media_item.upload")[i].data("info"));` will work but it doesn't – Yes Jul 19 '16 at 21:35
  • 1
    yeah [n] gives you a dom node, not a jquery object. dom nodes don't have jquery methods. – Kevin B Jul 19 '16 at 21:35
  • @KevinB, what do you suggest to do then? could you point me to the right direction? – Yes Jul 19 '16 at 21:37
  • Yeah, use the appropriate jquery method for getting n element out of y collection. `y.eq(n)` – Kevin B Jul 19 '16 at 21:38
  • omg! thank you @KevinB. Out of curiosity, how am I supposed to know that is the issue when I don't know the cause of the problem?? can't thank you enough. I will update the post and answer my question :) – Yes Jul 19 '16 at 21:45
  • 1
    well, the `.data` is not a function error should clue you in that you're trying to call a method that doesn't exist. at that point you should investigate *why* the method doesn't exist, and compare it to situations where it *does* exist. – Kevin B Jul 19 '16 at 21:47
  • I will try to remember this lesson. THANK YOU @KevinB – Yes Jul 19 '16 at 21:51

2 Answers2

0

Use the index-related selectors jquery method.

Didn't this method exist until @KevinB pointed it out. Another lesson though: I shouldn't keep digging on the internet to see how I can make it work but to investigate why it doesn't work. Investigating the situation is way more important.

Yes
  • 63
  • 5
-1
$(function () {    
    alert($(".media_item.upload").attr("data-info"));    
});
R. Richards
  • 24,603
  • 10
  • 64
  • 64
Rassel
  • 33
  • 3