Link to home
Start Free TrialLog in
Avatar of badwolfff
badwolfffFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How do I remove a certain text element from a div using jQuery if it is only appears a certain number of times?

I have a dynamically generated div that looks like (two random examaples shown below):

<div class="col-lg-6 col-sm-6 col-xs-12" id="association_content">
  <img src="http://www.missionfamily.org/image/cache/catalog/marchio_fas-270x269.jpg">
  <!--<img src="image/cart-ban.png"/>-->
  <h1>FAS - Fondo Ambiente Sammarinese</h1>
  <br>	
  &nbsp;&nbsp;|&nbsp;&nbsp;<strong><i class="fa fa-envelope-o"></i></strong> <a href="mailto:info@fondoambiente.sm?Subject=Messaggio%20da%20Mission%20Family:%20">info@fondoambiente.sm</a>	
</div>

Open in new window


or

<div class="col-lg-6 col-sm-6 col-xs-12" id="association_content">
  <img src="http://www.missionfamily.org/image/cache/catalog/MarchioPolisportivaStella-270x269.gif">
  <!--<img src="image/cart-ban.png"/>-->
  <h1>Polisportiva Stella S.G.</h1>
  Via Abruzzo, 36 47923 Rimini RN<br>	
  <strong><i class="fa fa-phone"></i></strong> 0541781283 &nbsp;&nbsp;|&nbsp;&nbsp;<i class="fa fa-mobile"></i> 0541781283	
  &nbsp;&nbsp;|&nbsp;&nbsp;<strong><i class="fa fa-envelope-o"></i></strong> <a href="mailto:segreteria@polstella.com?Subject=Messaggio%20da%20Mission%20Family:%20">segreteria@polstella.com</a>	
</div>

Open in new window




What I need to do is check if the string &nbsp;&nbsp;|&nbsp;&nbsp; appears more than once or not. If it appears ONLY ONCE, then I need to remove it, otherwise it can stay as it is. I need to remove just that string leaving the rest of the DIV as it is.

I've been trying to detect this using: $('#association_content').text().indexOf('&nbsp;&nbsp;|&nbsp;&nbsp;')
but I keep getting -1 in the console.log

thanks in advance
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Instead of .text() use .html()
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial