August 4, 2015 10:49 am

Useful HTML5 Features, Part 5

CSS-like JavaScript selectors HTML5 brought us CSS-like JavaScript selectors and they have a pretty decent support. This is really handy and was one of the main benefits of the jQuery library. The lengthy JavaScript selectors such asdocument.getElementsByClassName("container")[0].getElementsByTagName("p")[0] are on the way to history.
Useful HTML5 Features

Browser support

The following browsers support the querySelector property:

  1. IE8 has partial support
  2. IE9+ supports it fully.
  3. Firefox 31+
  4. Chrome 31+
  5. Safari 7+
  6. Opera 29+

Generally, it is supported by 96.92% of the Internet users out there so you can decide to use it without any fallbacks (statistics can be found in Can I Use).

Examples

First, we add some basic markup:

We can use querySelector and querySelectorAll. querySelector returns the first element that matches the given string with a CSS selector while querySelectorAll returns us all elements that match the CSS selector.

If we did not use querySelector and wanted to get that paragraph (which does not have a class on its own) we would have to type something like: document.getElementsByClassName("container")[0].getElementsByTagName("p")[0]… What a relief!

If we want to get both (all) paragraphs with the div with class of container we could use:

Cool things you can do with CSS3 attribute selectors

We are going to show you how you can easily add an image or text to a link indicating that it is a .pdf, .doc file or whatever you like. We are also going to show how you can signal the users that a particular link will move them away from your website.

In our markup, we have a div with links:

Firstly, we will add a .pdf icon to the links that lead to a .pdf file:

a[href$='.pdf'] is instructing the browser to search for anchors which have a href attribute whose value ends with .pdf. $= followed by text searches the attribute for a value that ends with the provided text.

We have used the :after pseudo-element and its feature content to add an image to the end of each matched anchor.

Let us repeat the process with the files that end with .doc:

If you see the links now you will see that <a href="pdf4.docx">What do men know about women</a> does not have a Microsoft Word icon. This is because it does not end with .doc, it ends with .docx.

Now, to add an external icon to the websites pointing to other domains we would have to use another attribute selector that seeks the beginning of an attribute for a certain value: ^=.

This searches for any anchor with a href attribute that starts with http (it could be http:// or https://). Notice also that this time we insert the icon before the content of the anchor itself with the :before pseudo-element.

Here is what we end up with:

screenshot html5 feature 5

Conclusion

I hope you found the today’s piece of HTML5 useful indeed. If you found this article interesting or you have questions, do not hesitate to write a comment. I will be glad to reply to any enquiries.

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter


Tutorial Categories:
  • Rui

    Great article