Moved
Moved

Reputation: 130

Dealing with IDs with blank spaces (Cytoscape.js)

I'm implementing a prerequisite graph using Cytoscape.js. But the problem is, when I set the id with the course name, (for example: Beginning Programming), I can't properly select the node because of the blank space in the course name.

temp.group = "nodes";
temp.data = {id: a, label: b}; // A: "Beginning Programming" B: "1111"
cy.add(temp);

Then, when I do this:

cy.$("Beginning Programming");

It says it is an invalid selector.

Is there a way of doing so?

Upvotes: 3

Views: 815

Answers (2)

Moved
Moved

Reputation: 130

After few hours of researching, I found that attribute selector works. The following code works like a charm.

cy.$("[id='Beginning Programming']");

Upvotes: 1

Ed Ballot
Ed Ballot

Reputation: 3485

You cannot use spaces in the id (see Can a DOM element have an ID that contains a space?).

I'd recommend replacing the space with underscore like this

var modifiedId = a.split(' ').join('_');
temp.data = {id: modifiedId , label: b}; // A: "Beginning_Programming" B: "1111"

If the id is also displayed, you could replace it with %20 (which is displayed as a space in html)

Upvotes: 0

Related Questions