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

JavaScript

data-url: removing spaces help

Hello,

Need some help removing spaces from a data-url attribute. I am using some jQuery to generate some dynamic content from an RSS feed into a data-url tag; however, I need to remove the spaces present within that generated text that replace the spaces with hyphens.

My problem is two-fold. I know I can use. replace(/\s+/g, "-") to accomplish part of my objective, but I am having a hard time implementing the code because of its location inside a data-url tag.

Second big problem. I have a TON of the these data-url tags. Thus, when I try to re-insert the modified code, it pull ALL of my data-url tags into each tag. I need the text of each data-url to remain as it is, where it is, minus the spaces. If anyone has advice or can help with the code, I would really appreciate it!

Thus, in some, I have this:

data-url="www.example.com/2013/06/04/This is an example" data-url="www.example2.com/2013/07/04/This is an example2"

Need:

data-url="www.example.com/2013/06/04/this-is-an-example" data-url="www.example2.com/2013/07/04/this-is-an-example2"

Thanks

1 Answer

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

You can use the .data() method documented here http://api.jquery.com/data/

I'd do this:

  1. Cycle over each element
  2. In the each anonymous function store $(this).data("url") as var url
  3. Modify the url with the replace code
  4. Update the url with $(this).data("url", url)

Hope that helps.