Forum Moderators: open

Message Too Old, No Replies

jQuery: getting last value of parent data-attribute

         

csdude55

8:07 am on Apr 17, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have a series of objects that have an attribute of data-b='[0-9]', and I'm using jQuery to determine what the last value in the list is, up to that point. Here's what I have:

<div data-b="0">
<div></div>
</div>

<div data-b="1">
<div></div>
</div>

<div data-b="2">
<div></div>
</div>

<script>
var lB = $('div').last().data('b');
alert(lB);
</script>


Here's the craziness: as it's written above, I get an alert of "undefined". But if I take out the empty child <div></div>, it alerts with the number 2, as expected.

I also tried using $('div').last().attr('data-b');, but had the same result.

This Javascript (not jQuery) DID work. I haven't benchmarked it, though, and I think it will be about 1 million times slower than last():

var lB = 0;

for (var i=0; i < document.getElementsByTagName('div').length; i++) {
var el = document.getElementsByTagName('div')[i];

if (el.getAttribute('data-b'))
lB = el.getAttribute('data-b');
}

alert(lB);


My third thought is to modify each <div data-b='[0-9]'></div> to include <script>lB++</script> after it. But that would require a manual modification to every page, so I'm thinking of that as a last resort.

Can you guys suggest a better way of getting this value?

Fotiman

1:03 pm on Apr 17, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Instead of using jQuery to get ALL 'div' elements, modify your selector to get all elements that have a data-b attribute:

alert($('[data-b]').last().data('b'));

Fotiman

1:14 pm on Apr 17, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Non-jQuery approach:

alert(Array.prototype.slice.call(document.querySelectorAll('[data-b]')).pop().dataset.b);

Note, this example is very basic, and could result in an error (if, for example, there are no elements with data-b attriubute). It wouldn't be difficult to extend this to add some safety checks.

csdude55

7:47 pm on Apr 17, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Awesome, Fotiman, that works perfectly!

Do you know of any particular reason why I should use the non-jQuery approach over the jQuery one?

Fotiman

8:16 pm on Apr 17, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It mostly depends on how much you're using jQuery already, and whether you care about being dependent on a 3rd party piece of software. jQuery has always been great for ease of DOM traversing and manipulation, so if you're already using it for other things, then I think it would be fine to continue using it. It's certainly more terse than the non-jQuery version (and as I mentioned, the non-jQuery version could use with an undefined test before attempting to access dataset).

If you're not already relying on jQuery for other things, then it probably doesn't make sense to include it just for this case.