Dominator.js | Attr Property (Nesting)

A modern approach to DOM manipulation using Dominator Nesting

Using the 'attr' Property with Dominator Nesting

The 'attr' property in Dominator.js allows you to directly set attributes for an element. When used with nesting, it helps you assign attributes to elements as they are created and appended, simplifying the process.

Example Usage:

1. Create an anchor tag with an href attribute and append it to .example-output


                    D$({element: 'a', text: 'Visit Google', attr: {href: 'https://www.google.com'}, appendTo: '.example-output'});
                

This code creates an <a> element, sets the `href` attribute, and appends it to the .example-output element.

2. Create an image tag with src and alt attributes, and append it to .example-output


                    D$({element: 'img', attr: {src: 'image.jpg', alt: 'Example Image'}, appendTo: '.example-output'});
                

This code creates an <img> element, sets the `src` and `alt` attributes, and appends it to the .example-output element.

The 'attr' property is used to define the attributes for each element. By passing an object containing attribute names and values, you can easily manage attributes for newly created elements in a nested fashion.