{"id":150476,"date":"2023-07-10T11:42:22","date_gmt":"2023-07-10T15:42:22","guid":{"rendered":"https:\/\/optinmonster.com\/?post_type=optinmonster_docs&#038;p=150476"},"modified":"2025-08-14T13:00:15","modified_gmt":"2025-08-14T17:00:15","slug":"how-to-use-optinmonsters-html-targeting-rule","status":"publish","type":"optinmonster_docs","link":"https:\/\/optinmonster.com\/docs\/how-to-use-optinmonsters-html-targeting-rule\/","title":{"rendered":"How to Use OptinMonster&#8217;s HTML Element Targeting Rule"},"content":{"rendered":"\n<p>OptinMonster makes it easy to hide or show campaigns based on an HTML element in the page the campaign is loading on.<\/p>\n\n\n\n<p>In this article, you&#8217;ll learn how to use OptinMonster&#8217;s Display Rules to hide or show a campaign based on an HTML element.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column alert alert-info is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong><em>Before you start<\/em><\/strong>, this feature requires a <a href=\"https:\/\/optinmonster.com\/pricing\" target=\"_blank\" rel=\"noopener\" title=\"Pro and higher subscription\">Growth subscription<\/a>.<\/p>\n<\/div>\n<\/div>\n\n\n\n<p>This rule can only target an element that already exists on your website.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configure HTML Element Rule<\/h2>\n\n\n\n<p>When you&nbsp;<a href=\"https:\/\/optinmonster.com\/docs\/creating-your-first-optin\/\" target=\"_blank\" rel=\"noopener\" title=\"\">create a campaign<\/a> in OptinMonster you can configure it to be shown or hidden based on an HTML element in the page.<\/p>\n\n\n\n<p>To configure the HTML element rule, follow these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Navigate to the <strong>Display Rules<\/strong> view in the campaign builder.<br><img decoding=\"async\" width=\"800\" height=\"560\" class=\"alignnone size-full wp-image-180093\" src=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/06\/display-rules-view.jpg\" alt=\"Navigate to the Display Rules in the OptinMonster campaign builder.\" srcset=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/06\/display-rules-view.jpg 800w, https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/06\/display-rules-view-300x210.jpg 300w, https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/06\/display-rules-view-768x538.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/li>\n\n\n\n<li>Next, when adding or editing a rule select <strong>Who (Personalization)<\/strong> from the left-hand list and select the <strong>HTML Element Targeting<\/strong> option.<br><img decoding=\"async\" width=\"800\" height=\"564\" class=\"alignnone size-full wp-image-180386\" src=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/html-targeting.jpg\" alt=\"HTML Element Targeting in OptinMonster.\" srcset=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/html-targeting.jpg 800w, https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/html-targeting-300x212.jpg 300w, https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/html-targeting-768x541.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/li>\n\n\n\n<li>Configure the rule as desired using the operator dropdown and text field.<br><img decoding=\"async\" width=\"766\" height=\"112\" class=\"alignnone size-full wp-image-180385\" src=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/html-rule-options.jpg\" alt=\"Configure the HTML Element rule in OptinMonster.\" srcset=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/html-rule-options.jpg 766w, https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/html-rule-options-300x44.jpg 300w\" sizes=\"(max-width: 766px) 100vw, 766px\" \/><br>The following options are available for HTML Element Targeting:\n<ul class=\"wp-block-list\">\n<li>matches selector<\/li>\n\n\n\n<li>does not match selector<\/li>\n\n\n\n<li>is visible, and matches selector<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>When you&#8217;re finished, <strong>Save<\/strong> the campaign.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Examples<\/h2>\n\n\n\n<p>The following examples are to help you get started:<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Exactly Matches Selector<\/h5>\n\n\n\n<p>If you wanted to check for an HTML element with an ID of <code>testHTMLelementID<\/code>, you would enter <code>#testHTMLelementID<\/code> as the value, as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"748\" height=\"112\" src=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/html-matches.jpg\" alt=\"HTML Element example.\" class=\"wp-image-180389\" srcset=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/html-matches.jpg 748w, https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/html-matches-300x45.jpg 300w\" sizes=\"(max-width: 748px) 100vw, 748px\" \/><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">Does Not Exactly Match Selector<\/h5>\n\n\n\n<p>If you wanted to only show the campaign when a certain element does NOT exist on the page, you can use the <em>does not match selector<\/em>&nbsp;operator. Since the value of the input can be any valid query selector, you can be quite creative with this rule.<\/p>\n\n\n\n<p>In this example, we&#8217;ll only show the campaign when a certain HTML element&nbsp;<em>does not exist on the page<\/em>. Using the&nbsp;<em>does not match<\/em> operator, we&#8217;ll check if an element with the ID of <code>testID<\/code> that is a child of any element with a class name of <code>className<\/code> exists on the page.<\/p>\n\n\n\n<p>If any elements matching this query are found on the page, the campaign will not display.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"750\" height=\"112\" src=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/html-does-not-match.jpg\" alt=\"HTML Element example.\" class=\"wp-image-180388\" srcset=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/html-does-not-match.jpg 750w, https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/html-does-not-match-300x45.jpg 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<h5 class=\"wp-block-heading\">Can I learn more about how this Display Rule works?<\/h5>\n\n\n\n<p>You can enter a key (<em>matches selector<\/em> or&nbsp;<em>does not match selector<\/em>) to search for in the page source.<\/p>\n\n\n\n<p>OptinMonster then uses document.querySelector to search for whatever string you&#8217;ve entered in the Display Rule. If that string is or is not found, depending on the selected operator, the campaign will show.<\/p>\n\n\n\n<p>This Display Rule is especially helpful for more technical users since you can set up your own conditions using custom Javascript or HTML and then only display the campaign if that HTML element is detected.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">How does the &#8220;is visible and matches selector&#8221; operator work?<\/h5>\n\n\n\n<p>When using this rule it will check your query selector and then determine if the targeted element exists.<\/p>\n\n\n\n<p>If the element exists, it will then determine if the element is visible to the visitor.<\/p>\n\n\n\n<p>If the element is hidden, or the visitor hasn&#8217;t scrolled down enough to see it yet, the campaign will not display.<\/p>\n\n\n\n<p>Once the visitor has scrolled to the targeted element, or the targeted element otherwise becomes visible in the visitor&#8217;s viewport, the campaign will display.<\/p>\n\n\n","protected":false},"excerpt":{"rendered":"<p>OptinMonster makes it easy to hide or show campaigns based on an HTML element in the page the campaign is loading on. In this article, you&#8217;ll learn how to use OptinMonster&#8217;s Display Rules to hide or show a campaign based on an HTML element. This rule can only target an element that already exists on&nbsp;&hellip;<\/p>\n","protected":false},"author":4657,"comment_status":"open","ping_status":"closed","template":"","documentation\/categories":[28201],"documentation\/tags":[31399,31498,31415,31856,31459,31411],"class_list":["post-150476","optinmonster_docs","type-optinmonster_docs","status-publish","hentry","categories-display-rules"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/documentation\/150476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/documentation"}],"about":[{"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/types\/optinmonster_docs"}],"author":[{"embeddable":true,"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/users\/4657"}],"replies":[{"embeddable":true,"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/comments?post=150476"}],"version-history":[{"count":16,"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/documentation\/150476\/revisions"}],"predecessor-version":[{"id":194445,"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/documentation\/150476\/revisions\/194445"}],"wp:attachment":[{"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/media?parent=150476"}],"wp:term":[{"taxonomy":"categories","embeddable":true,"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/documentation\/categories?post=150476"},{"taxonomy":"internal_tags","embeddable":true,"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/documentation\/tags?post=150476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}