Data attributes in Oxygen

As of Oxygen 2.2.1, there is no built-in provision for adding data attributes to elements.

While my earlier tutorial on this topic did provide a working solution to this, it is clunky compared to the simplicity of the method in this article.

We can add a Code Block and utilize jQuery’s attr property like this:

$('#link_text-19-224').attr('data-featherlight', '#lightbox');

Details

In your Oxygen Template/editor, add a Code Block component at the end of all others.

PHP & HTML:

<?php
	// echo "hello world!";
?>

JavaScript:

(function($) {
'use strict';

	$('#link_text-19-224').attr('data-featherlight', '#lightbox');

}(jQuery));

Replace link_text-19-224 with the ID of your element.

With the above code in place, we are telling the browser to wait until the webpage (DOM – Document Object Model) loads and then run the code inside an anonymous function that takes in jQuery as a parameter and runs the code replacing all instances of $ with the passed parameter, jQuery.

Info on use strict can be seen here.

For adding data attributes to multiple elements, simply add more lines like this:

(function($) {
'use strict';

	$('#section-2-224').attr('data-lax-preset', 'spin fadeInOut');
	$('#link_text-19-224').attr('data-featherlight', '#lightbox');

}(jQuery));

Posted

in

,

by

Comments

5 responses to “Data attributes in Oxygen”

  1. Paul Avatar
    Paul

    Brilliant, thank you!

  2. aaa Avatar
    aaa

    What’s the purpose and real life usage for this???

    1. Sridhar Katakam Avatar

      Data attributes are some times necessary to be added to elements to do certain tasks like launching lightboxes, setting up scrolling animations etc.

Leave a Reply to How to add rel="dofollow" to social icons in Oxygen – WPDevDesign Cancel reply

Your email address will not be published. Required fields are marked *