japes Sophey
japes Sophey

Reputation: 495

jQuery syntax error, unrecognised expression

syntax error, unrecognised expression: #2015-11-30|1112|1

I have an anchor tag with an Id of '2015-11-30|1112|1' that I would like to apply a class to. I am doing the same method for on a '' and this works, but I am getting syntax errors with the following. Can anyone explain the syntax error?

   $(document).ready(function() {
        $("#tbl_calendar").on("click", "a", null, clickAppointment);
    });


function clickAppointment(eventData)
    {
        //Get the Id of the appointment that has been clicked:
        currentAppointment = $(this).attr('id');

        //alert('clicked' + '#'+currentAppointment)

        $('#'+currentAppointment).addClass('selected');
    }

Upvotes: 2

Views: 231

Answers (2)

Zakaria Acharki
Zakaria Acharki

Reputation: 67495

You should escape the special chracters in your id using \\, check example bellow.

Hope this helps.


console.log( $("#2015-11-30\\|1112\\|1").text() );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="2015-11-30|1112|1">Div text example</div>

Upvotes: 4

rahul
rahul

Reputation: 187010

For your current code to work, you don't have to use that id selector since you already have the reference of the object inside the event function.

$(document).ready(function() {
  $("#tbl_calendar").on("click", "a", clickAppointment);

  function clickAppointment(eventData) {
    //"this" will have a reference to the clicked object
    $(this).addClass("selected");
  }
});

Not sure about your HTML, but considering something similar to the below one.

<ul id="tbl_calendar">
  <li>
    <a id="2015-11-30|1112|1">Click</a>
  </li>
</ul>

Working sample

Upvotes: 2

Related Questions