1

I'm missing something simple here, I need to collect all the data-event attrs in a string array, is there a wrapper issue for elements or something I need to be aware of? this is not working:

var dataEvents = $('li').map(function(el) {
     return $(el).attr('data-event');
});

console.log('DataEvents: ' + dataEvents[0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li data-event="abc">TEST 1</li>
<li data-event="def">TEST 2</li>
<li data-event="ghi">TEST 3</li>

0

1 Answer 1

1

For this use jQuery.map()

var dataEvents = $.map($("li"), function(el) {
     return $(el).attr('data-event');
});

console.log(dataEvents);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li data-event="abc">TEST 1</li>
<li data-event="def">TEST 2</li>
<li data-event="ghi">TEST 3</li>

Sign up to request clarification or add additional context in comments.

1 Comment

Your difference is you used $.map whereas I used array.map(..).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.