Title: toggleClass by tag name
Last modified: February 19, 2021

---

# toggleClass by tag name

 *  Resolved [Taras Dashkevych](https://wordpress.org/support/users/taskotr/)
 * (@taskotr)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/toggleclass-by-tag-name/)
 * Hello,
 * Is it possible to use toggleClass action on element without ID attribute?
 * I know that it’s possible when the element has ID=”body”:
 *     ```
       on="tap:body.toggleClass(class=active-sidebar)"
       ```
   
 * What if I want to toggle class for a body element that does not have ID attribute,
   is it possible?
 * Thank you,
    Taras

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [Weston Ruter](https://wordpress.org/support/users/westonruter/)
 * (@westonruter)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/toggleclass-by-tag-name/#post-14071589)
 * Hi. No, if you want to use the `toggleClass` action you need to give the target
   element an ID.
 * I suggest giving the `body` an ID. We are doing this in the core theme sanitizer
   that adds toggles to the Twenty Twenty theme, which also involves the use of `
   toggleClass`.
 * [https://github.com/ampproject/amp-wp/blob/26034a78fadfb28c7f2bdb1e3004dc4136cad0c9/includes/sanitizers/class-amp-core-theme-sanitizer.php#L1916](https://github.com/ampproject/amp-wp/blob/26034a78fadfb28c7f2bdb1e3004dc4136cad0c9/includes/sanitizers/class-amp-core-theme-sanitizer.php#L1916)
   
   [https://github.com/ampproject/amp-wp/blob/26034a78fadfb28c7f2bdb1e3004dc4136cad0c9/includes/sanitizers/class-amp-core-theme-sanitizer.php#L1985](https://github.com/ampproject/amp-wp/blob/26034a78fadfb28c7f2bdb1e3004dc4136cad0c9/includes/sanitizers/class-amp-core-theme-sanitizer.php#L1985)
 *  Thread Starter [Taras Dashkevych](https://wordpress.org/support/users/taskotr/)
 * (@taskotr)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/toggleclass-by-tag-name/#post-14071658)
 * Hi Weston,
    Thank you very much for info! You are very helpful as always 🙂
 * I’ve noticed that a default markup of 2020 does not include ID in a body element.
   It looks like this id (body-0) is added automatically by AMP plugin only in mobile
   views.
 * Do you have information, or maybe link to a code, on how to properly implement/
   set it in a WordPress theme?
 * Does [this function automatically creates](https://github.com/ampproject/amp-wp/blob/26034a78fadfb28c7f2bdb1e3004dc4136cad0c9/includes/sanitizers/class-amp-core-theme-sanitizer.php#L1916)
   ID for a body element?
 * Kind regards,
    Taras
 *  Plugin Author [Weston Ruter](https://wordpress.org/support/users/westonruter/)
 * (@westonruter)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/toggleclass-by-tag-name/#post-14071750)
 * Yes, that function automatically creates the ID for the body element.
 * If there is already an ID set on the body, then that function just returns whatever
   was already set on it.
 * So you can just go ahead and add the desired `id` attribute to the `<body>` in
   your theme template, if you’re working on a theme. For example:
 * `<body id="body" <?php body_class(); ?>>`

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘toggleClass by tag name’ is closed to new replies.

 * ![](https://ps.w.org/amp/assets/icon.svg?rev=2527602)
 * [AMP](https://wordpress.org/plugins/amp/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/amp/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/amp/)
 * [Active Topics](https://wordpress.org/support/plugin/amp/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/amp/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/amp/reviews/)

## Tags

 * [element](https://wordpress.org/support/topic-tag/element/)

 * 3 replies
 * 2 participants
 * Last reply from: [Weston Ruter](https://wordpress.org/support/users/westonruter/)
 * Last activity: [5 years, 2 months ago](https://wordpress.org/support/topic/toggleclass-by-tag-name/#post-14071750)
 * Status: resolved