Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select an element that contains " " within its HTML?

Tags:

jquery

For example, in this HTML:

<div class="el"> &nbsp ;&nbsp ;&nbsp ;&nbsp ;HasNbsp</div>
<div class="el">NoNbsp</div>

I would like to select the element that has "&nbsp" in it.

I tried this:

$("div.el").html().contains("&nbsp;")

But it's not working.

How can I correctly target the element that contains a non-breaking space?

Note: I need to keep the non-breaking space, so removing it isn't an option.

like image 787
Xawi Leones Avatar asked Jan 08 '23 16:01

Xawi Leones


1 Answers

Use \u00a0 in your selector instead of &nbsp;

So in your case, you want this:

$("div.el").html().contains("\u00a0")

Here's a CodePen with some more example code:

https://codepen.io/anon/pen/QJpPJV

like image 116
Pikamander2 Avatar answered May 25 '23 19:05

Pikamander2