Element selectors used in the Visual Experience Composer vec-selectors

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.
NOTE
Element classes are available as selectors in A/B Test, Automated Personalization, and Multivariate Test activities.

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:

  1. 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>
    
  2. 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).

    eq gives the index of the element that has “tagName=UL” and the first class navigation. Therefore, index is 0. See the Selectors article in MDN for more information.

  3. If an element does not contain a class, Target uses tagName for 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.
recommendation-more-help
target-help-main