On this page: Learn how element selectors work in the Visual Experience Composer (VEC), how to choose which selectors are used, and how Target generates a selector for an element.
An element selector is a CSS expression that can identify one or more elements. Basic information about CSS selectors is available in the Selectors document on the Mozilla Developer Network (MDN).
Whether to use element classes or element IDs is set in the account preferences. Click Administration > Visual Experience Composer, then choose the preferred CSS selectors:
- Use element IDs: Disable if the same ID is used for multiple elements, or if element IDs might change on page load.
- Use element classes: Disable if element classes on a page might change.
- Use preferred selectors: Enable to use unique selectors in the VEC to identify key areas of the website.
For information about when to use CSS selectors and when to use unique IDs, see Visual Experience Composer best practices and limitations.
How Target generates a selector for an element generate-selector
Target uses a simple algorithm to create a selector. The generation logic is as follows:
-
If an element has an ID, for example
id="container", the selector for the element is#container.For example:
code language-html <div class="wrapper"> <div id="container"> <!-- Selector is computed for this element --> <ul class="navigation"> <li class="item active"> Home </li> <li class="item"> Men </li> <li class="item"> Women </li> <li class="item"> Kids </li> </ul> </div> </div> -
If an element contains a class attribute, Target attempts to use the first class of any classes present on the element.
Target attempts to parse the parent element until it finds the
<HTML>element or an element with an ID. Whenever an element contains an ID and the selector is computed on its descendant child, the ID of this element contributes to the selector.For example:
code language-html <div class="wrapper"> <div id="container"> <!-- id is present here. It contributes to selector --> <ul class="navigation"> <li class="item active"> Home </li> <!-- Selector is computed for this element --> <li class="item"> Men </li> <li class="item"> Women </li> <li class="item"> Kids </li> </ul> </div> </div>In this example, the selector is
#container>ul.navigation:eq(0)>li.item:eq(0)(" > " indicates the immediate child).eqgives the index of the element that has “tagName=UL” and the first classnavigation. Therefore,indexis 0. See the Selectors article in MDN for more information. -
If an element does not contain a class, Target uses
tagNamefor the element and traverses up the parent element until either the<HTML>element or an element with an ID is found.For example:
code language-html <div class="wrapper"> <div id="container"> <!-- id is present here. It contributes to selector --> <ul class="navigation"> <li> Home </li> <li> Men </li> <li class="active"> Women </li> <li> Kids </li><!-- Selector is computed for this element --> </ul> </div> </div>The selector is
#container>ul.navigation:eq(0)>li:nth-of-type(4).
In this process:
- Any CSS selector can be used, as long as it uniquely identifies an element in the DOM.
- The approach above is the one used by Target. Target does not mandate this approach. Any selector can be added, as long as it uniquely identifies the element.
- Any attribute can be used in the selector. This article uses only a class name as an example.
This section contains structured knowledge intended to support interpretation, retrieval, and question answering related to this topic.
For complete understanding, this information should be combined with the documentation on this page. Neither source is intended to stand alone; the page describes the feature, while this section provides additional context that helps disambiguate terminology, intent, applicability, and constraints.
- TL;DR: This page explains how element selectors work in the Visual Experience Composer (VEC), how to choose which selectors are used, and how Target generates a selector for an element.
Intents:
- Understand what an element selector is in the Visual Experience Composer (VEC).
- Choose which CSS selectors are used, through Administration > Visual Experience Composer in the account preferences.
- Decide when to disable Use element IDs or Use element classes.
- Understand how Target generates a selector for an element that has an ID, a class, or no class.
- Understand which selectors can be used: any CSS selector that uniquely identifies an element, and any attribute in the selector.
Glossary:
- Element selector: a CSS expression that can identify one or more elements.
- Use preferred selectors: a setting in Administration > Visual Experience Composer that, when enabled, uses unique selectors in the VEC to identify key areas of the website. (product-specific)
Applicability:
- Element classes are available as selectors in A/B Test, Automated Personalization, and Multivariate Test activities.
Terminology:
- Canonical name: Visual Experience Composer
- Acronym: VEC (Visual Experience Composer)
FAQ:
- Where do I choose whether element classes or element IDs are used? In the account preferences: click Administration > Visual Experience Composer, then choose the preferred CSS selectors.
- When should I disable Use element IDs? Disable it if the same ID is used for multiple elements, or if element IDs might change on page load.
- When should I disable Use element classes? Disable it if element classes on a page might change.
- Does Target require its own selector generation approach? No. Target does not mandate the approach described on the page; any selector can be added, as long as it uniquely identifies the element.
- Which attributes can be used in a selector? Any attribute can be used in the selector. The page uses only a class name as an example.
Examples: (each one shows how Target generates a selector for an element)
- Element with an ID: if an element has an ID, for example
id="container", the selector for the element is#container. - Element with a class attribute: Target attempts to use the first class of any classes present on the element, and attempts to parse the parent element until it finds the
<HTML>element or an element with an ID. Whenever an element contains an ID and the selector is computed on its descendant child, the ID of this element contributes to the selector. For the firstliwith the classesitem activeinsideul class="navigation"insidediv id="container", the selector is#container>ul.navigation:eq(0)>li.item:eq(0)(" > " indicates the immediate child).eqgives the index of the element that has “tagName=UL” and the first classnavigation, soindexis 0. - Element without a class: Target uses
tagNamefor the element and traverses up the parent element until either the<HTML>element or an element with an ID is found. For the fourthli(without a class) insideul class="navigation"insidediv id="container", the selector is#container>ul.navigation:eq(0)>li:nth-of-type(4).
Related pages:
- When to use CSS selectors and when to use unique IDs: Visual Experience Composer best practices and limitations (c-experiences/c-visual-experience-composer/experience-composer-best-practices.md)
Personas:
- admin: Can choose which CSS selectors are used in the account preferences, by selecting Administration > Visual Experience Composer. Can disable Use element IDs if the same ID is used for multiple elements, or if element IDs might change on page load. Can disable Use element classes if element classes on a page might change. Where: “FAQ”.
- developer: Can add any selector, as long as it uniquely identifies the element, and can use any attribute in the selector. The page states that Target does not mandate the approach described on the page and uses only a class name as an example. Where: “FAQ”.
Onboarding:
- Level: Experienced
- First minimal step: Select Administration > Visual Experience Composer in the account preferences, then choose the preferred CSS selectors.
- Next: Learn when to use CSS selectors and when to use unique IDs: Visual Experience Composer best practices and limitations (c-experiences/c-visual-experience-composer/experience-composer-best-practices.md)