Skip to content

Instantly share code, notes, and snippets.

@yayMark
Created March 9, 2019 23:44
Show Gist options
  • Save yayMark/3065529f22855fc317ca652bc717bee2 to your computer and use it in GitHub Desktop.
Save yayMark/3065529f22855fc317ca652bc717bee2 to your computer and use it in GitHub Desktop.
WordPress, Elementor: Get to a parent element of a Elementor element that I attach a class to. I wish I didn't have to do this.
function findParentTagId(childClass, parentTag) {
var element = document.querySelector(childClass);
var tag = element.tagName;
// repeat until you get to body
while (tag != 'body') {
element = element.parentElement;
if (element.tagName.toLowerCase() === parentTag.toLowerCase()) {
var dataset = element.dataset['id'];
return document.querySelector('[data-id="' + dataset + '"]');
}
tag = element.parentElement.tagName;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment