An element selector is a CSS expression which can identify one or more elements.
You can find basic information about CSS selectors in the Selectors document on the Mozilla Developer Network (MDN).
You can set whether to use element classed or element IDs in your account preferences. Click Administration > Visual Experience Composer, then choose your preferred CSS selectors.
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.
Target uses a simple algorithm to create a selector. Here is very brief explanation of the generation logic:
If an element has an id, for example id="container"
, then the selector for the element is #container
.
For Example:
<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 leverage 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, this element’s id contributes to the selector.
For example:
<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:
Selector: #container
> ul.navigation:eq(0)
> li.item:eq(0)
(" > " indicates the immediate child.)
eq
tells the index there’s an element that has “tagName=UL” and the first class is navigation
. Therefore, index
is 0. See the Selectors article in MDN for more information.
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:
<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>
Selector: #container
> ul.navigation(0)
> li:nth-of-type(4)
In the above process: