0

I have an HTML like this:

<div class='item'>
    <h1 id="C++">C++</h1>
    <p>some thing</p>
</div>

I want to use jQuery selector to select the p tag, I write:

$("#C+++p")

But It failed. Than I read the document about jQuery selector and made some change:

$("#C//+//+//+p")

It still not worked. What's the problem of my code? Thank you!

liyuhao
  • 335
  • 1
  • 2
  • 6

1 Answers1

4

In your example you used the wrong slashes. To escape characters in a jQuery selector string you need to use two backslashes \\.

$('#C\\+\\+')

After escaping you can add the rest of the selector

alert($('#C\\+\\+ + p').text())
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class='item'>
    <h1 id="C++">C++</h1>
    <p>some thing</p>
</div>

Note: You only need to escape the +'s that are part of the ID or class.

Spokey
  • 10,741
  • 2
  • 23
  • 40