Ivan
Ivan

Reputation: 15932

Jquery colon conflict

HTML allows to use as a valid identifier (ID) a colon. For example:

<div id="item:0">...</div>

The problem is I have a Jquery snippet like this:

$("#" + id).clone(true, true);

When some ID uses a colon this call makes JQuery crash. What can I do to make it work? (Note: changing colon symbol for other valid character is not an option)

Upvotes: 2

Views: 190

Answers (3)

simoncereska
simoncereska

Reputation: 3043

Can't you clone a div, not by id?

Upvotes: 0

Billy Moon
Billy Moon

Reputation: 58581

You say changing the id is not possible - but could you change the id with javascript client side?

Otherwise, you could assign a class to each element that has id with colon, and select them by their class.

Upvotes: 0

John Kurlak
John Kurlak

Reputation: 6680

Escape the id with something like

id = id.replace(':', '\\:');

before you try selecting it.

Upvotes: 6

Related Questions