0

I want to add or remove lis to a list in javascript. For that purpose I found that I need to add data attributes and find the item with that value.

if ($('li[data-requestId=' + requestId + ']').length == 0)
{
    var element = getRequestElement();
    var li = document.createElement("li");
    li.setAttribute("data-requestId",requestId);
    li.appendChild(element);
    $('#requestsUL').append(li);
}

but the first line gives this error:

Syntax error, unrecognized expression: li[data-requestId=6.22.20]

Blendester
  • 1,583
  • 4
  • 19
  • 43
  • 1
    @mplungjan you've incorrectly marked this as a duplicate; it's not necessary to escape the dots inside the `[]` part of the selector. `$('#x.y')` needs escaping as `$('#x\\.y')` to match `
    `; `$('[data-bar="x.y"]')` does *not* need escaping to match `
    `.
    – Daniel Beck Oct 08 '17 at 06:44
  • @mplungjan I tested it, placing the attribute inside the quotes does the trick even if the data attribute contains dots(`.`). – Hassan Imam Oct 08 '17 at 06:51