Solution 1 :

The issue is due to the + character in the selector which has a special meaning. You need to escape it using \.

Also note that your HTML is invalid. The first td needs to be within a tr.

console.log($('table#Evolución_Depósitos_a_Plazo_\+_Restringidos').length);
table {
  background: none repeat scroll 0% 0%;
  font-size: 12px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="1px" cellpadding="0" cellspacing="1" width="100%" id="Evolución_Depósitos_a_Plazo_+_Restringidos">
  <thead>
    <tr>
      <td align="center" colspan="17">Evolución_Depósitos_a_Plazo_+_Restringidos</td>
    </tr>
    <tr>
      <td class="center"></td>
      <td align="center" colspan="13">Evolución Tasa de Morosidad </td>
      <td align="center" colspan="3">Variacion %</td>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

Problem :

I have created an HTML table with a particular id but when I try to check if it exists or not it returns 0.

<table border = "1px"cellpadding="0" cellspacing="1" width="100%" id="Evolución_Depósitos_a_Plazo_+_Restringidos" %> style="background: none repeat scroll 0% 0%;font-size:12px;">
  <thead>
    <td align="center" colspan="17">Evolución_Depósitos_a_Plazo_+_Restringidos</td>
    <tr>
      <td class="center"></td>
      <td align="center" colspan="13">Evolución Tasa de Morosidad </td>
      <td align="center" colspan="3">Variacion %</td>
    </tr>
  </thead>
  <tbody></tbody>
</table>

I have used this code to check whether if it exists:
$(‘table#Evolución_Depósitos_a_Plazo_+_Restringidos’).length

However this returns 0. Help much appreciated. Thanks.

Comments

Comment posted by How do I select an element with special characters in the ID?

Does this answer your question?

Comment posted by atmin

Don’t use such chars in an id. No ‘+’ or ‘/’ or whatever. Only ASCII literals, ‘_’ and ‘-‘ is best practice. Ids should never start with a number or ‘-‘ due to CSS issues for some old browsers.

Comment posted by Amal

Thank you but I was forced to use this because it is dynamically generated and the id served a purpose that’s why. It is working fine with escaping characters it is ok right ?

Comment posted by Amal

@HereticMonkey Yes and I have selected that as the answer. I didn’t know that ‘+’ was a special character.

By

Leave a Reply

Your email address will not be published. Required fields are marked *