How do I get jQuery to select elements with a . (period) in their ID?

From Google Groups:

Use two backslashes before each special character.

A backslash in a jQuery selector escapes the next character. But you need
two of them because backslash is also the escape character for JavaScript
strings. The first backslash escapes the second one, giving you one actual
backslash in your string – which then escapes the next character for jQuery.

So, I guess you’re looking at

$(function() {
  $.getJSON("/Location/GetCountryList", null, function(data) {
    $("#Address\\.Country").fillSelect(data);
  });
  $("#Address\\.Country").change(function() {
    $.getJSON("/Location/GetRegionsForCountry", { country: $(this).val() }, function(data) {
      $("#Address\\.State").fillSelect(data);
    });
  });
});

Also check out How do I select an element by an ID that has characters used in CSS notation? on the jQuery FAQ.

Leave a Comment

tech