How to negate class selector in Cytoscape.js?

Viewed 1164

I want to select all elements that do not have the class "myclass". How can I do that in Cytoscape.js?

According to http://js.cytoscape.org/#selectors/data, "[^name] Matches elements if the specified data attribute is not defined", however a class is not a data attribute and ^.myclass does not work, neither does :not(.myclass).

The error is The selector :not(.myclass) is invalid.

Is there a way to negate classes?

2 Answers

If you want to get the negative class selector, you can do this:

cy.elements().not(cy.$('.yourClass'));

// in more detail
var allElements = cy.elements(); // get all elements
var negators = cy.$('.yourClass');  // get all elements with the class to negate
var result = allElements.not(negators); // gets the difference between the two collections

If you really want to achieve this by using selectors only, then you might add a data field to each element which has myclass (this can be done while adding the class), and then use [^myclass]

Related