Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Chris Thomas
Front End Web Development Techdegree Graduate 34,909 PointsStore attribute value in an array
Might seem like an easy jQuery question but here goes.
Say I have:
<a id="theLink" href="#" data-title="I am a string of words">My Link</a>
And I want to use jQuery to grab the string assigned to the data-title attribute and store it in an array variable where each element is one of the words.
var $myArray = $('#theLink').attr('data-title');
I guess I'm not really sure where to go from there.
1 Answer
Kai Nightmode
37,045 PointsAhh, you can leverage the wonderful String.split() method.
Here is the code that should do exactly what you want.
var $myArray = $('#theLink').data('title').split(' ');
Note: I'm using the data method from jQuery instead of attr since that worked perfectly in the version of jQuery I tested with.
Cheers! ^_^
Chris Thomas
Front End Web Development Techdegree Graduate 34,909 PointsChris Thomas
Front End Web Development Techdegree Graduate 34,909 PointsThank you!