notthehoff notthehoff - 1 year ago 184
jQuery Question

How can I get input tags with title attributes that contain " "?

I have the following input tag in my dom and unable to add classes and/or ids:

<input type="radio" title="&nbsp;">

When I try

$('input[title=" "]')



it does not return anything. Can I pull this based on that title?

Answer Source

Your selectors didn't work because you didn't use the proper symbol. The title attribute gets any html entities decoded, so &nbsp; is going to get decoded to the actual symbol   (ascii code 160). So that means the &nbsp; text isn't going to match it, neither will a regular space (ascii code 32).

On a Windows OS you can type ALT+0160 to get the symbol. On other OS you will need to find the equivalent key combo, or copy paste from a character map.

console.log( "Number of inputs with regular space: ", $('input[title=" "]').length );
console.log( "Number of inputs with &nbsp; text: ", $('input[title="&nbsp;"]').length );
console.log( "Number of inputs with &nbsp; symbol: ", $('input[title=" "]').length );
<script src=""></script>
<!-- Regular space in title -->
<input type="checkbox" title=" "> 

<!-- non-break space html entity -->
<input type="checkbox" title="&nbsp;"> 

<!-- encoded html entity which will match &nbsp;  -->
<input type="checkbox" title="&amp;nbsp;">

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download