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

How spaces are interpreted by jquery?

Hi, On the teacher's code he appends: ' <a href="#">EMAIL US</a>'

But when he removes it from the code, the space before the <a> tag was kept, as he removes only the <a>...</a>

When I look at the console, I see that there are no spaces there, even though we only removed the link.

Why does that happen? Thanks!

Hi @pedroferrari, just letting you know I've formatted your question to escape the HTML. Otherwise the HTML you're trying to describe is turned into actual HTML elements and therefore the tags can't be seen.

2 Answers

In HTML in general, whitespace is effectively compressed into just at most, a single space separating elements.

When you use jQuery's append method with a space in between or outside of an element, it's effectively adding a text node in addition to the element. Once the element has been removed though, the excess whitespace is mostly ignored, particularly if you're looking at the HTML in Chrome Dev Tools or similar, rather than the source file.

So, the space might be there, but because there's nothing following it, it's mostly ignored.

Thank you @iainsimmons

I am often afraid on counting on browsers to render something exactly the same way, but it looks like won't be a problem :)

I think cross-browser support is one of the reasons jQuery became, and still is, one of the most popular JavaScript libraries.