How to remove id with double colon via jQuery?

Viewed 79

How to remove an id with double colon?

<li role="treeitem" aria-selected="false" aria-level="2" aria-labelledby="Vendor_Name::users_anchor" aria-expanded="true" id="Vendor_Name::users" class="jstree-node  jstree-open">

What I tried:

$('[id*="Vendor_Name::users"]').remove();
$("#Vendor_Name\\:\\:users").remove();
$(document.getElementById("Vendor_Name::users")).remove();
$('li[id*="Vendor_Name::users"]').remove();
$('[id="Vendor_Name::users"]').remove();
$("li#Vendor_Name\\:\\:users").remove();

Nothing works. Removing other elements are working, so it has to do with ::

I can remove it via CSS, though, but I want to remove the element completely from DOM.

#Vendor_Name\:\:users {
  display: none;
}

EDIT: Got it working with MutationObserver. In case anybody needs it:

const DEL_SELECTOR = '#Vendor_Name\\:\\:users';

            const mo = new MutationObserver(onMutation);
// in case the content script was injected after the page is partially loaded
            onMutation([{addedNodes: [document.documentElement]}]);
            observe();

            function onMutation(mutations) {
                const toRemove = [];
                for (const {addedNodes} of mutations) {
                    for (const n of addedNodes) {
                        if (n.tagName) {
                            if (n.matches(DEL_SELECTOR)) {
                                toRemove.push(n);
                            } else if (n.firstElementChild && n.querySelector(DEL_SELECTOR)) {
                                toRemove.push(...n.querySelectorAll(DEL_SELECTOR));
                            }
                        }
                    }
                }
                if (toRemove.length) {
                    mo.disconnect();
                    for (const el of toRemove) el.remove();
                    observe();
                }
            }

            function observe() {
                mo.observe(document, {
                    subtree: true,
                    childList: true,
                });
            }
2 Answers

Could use jQuery.escapeSelector and then remove the element.

which Escapes any character that has a special meaning in a CSS selector.

const escapedSelector = $.escapeSelector("Vendor_Name::users");
console.info(escapedSelector); // Vendor_Name\:\:users

$('#' + escapedSelector).remove();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!DOCTYPE html>
<html>
<body>
<p>Test paragraph before list</p>
<li role="treeitem" aria-selected="false" aria-level="2" aria-labelledby="Vendor_Name::users_anchor" aria-expanded="true" id="Vendor_Name::users" class="jstree-node  jstree-open">Test list
</li>
</body>
</html>

note: That this method was added in jQuery version 3.

Solution for older version of jQuery: source taken from the jQuery core FAQ section

function jq( myid ) {
  return "#" + myid.replace( /(:|\.|\[|\]|,|=|@)/g, "\\$1" );
}
console.info(jq('Vendor_Name::users')) // #Vendor_Name\:\:users
$(jq('Vendor_Name::users')).remove()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

<!DOCTYPE html>
<html>
<body>
<p>Test paragraph before list</p>
<li role="treeitem" aria-selected="false" aria-level="2" aria-labelledby="Vendor_Name::users_anchor" aria-expanded="true" id="Vendor_Name::users" class="jstree-node  jstree-open">Test list
</li>
</body>
</html>

You can see from the following test of every example you gave that they are all valid and all find the element...assuming it exists when you run it.

The js-tree class indicates this is probably a tree plugin that uses an internal state to render various nodes. You probably want to use the API of the plugin to do what is needed

const selectors = [
    '[id*="Vendor_Name::users"]',
    "#Vendor_Name\\:\\:users",
    document.getElementById("Vendor_Name::users"),
    'li[id*="Vendor_Name::users"]',
    "li#Vendor_Name\\:\\:users"
]

const allValid = selectors.every(s=> $(s).length > 0)
console.log('allValid =', allValid)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li id="Vendor_Name::users" class="jstree-node  jstree-open">Test</li>
</ul>

Related