JQuery count specifier in div

I have php code that generates tags <a>, and the final output is as follows:

<div id='link1'>
<a style="font-size:10pt; color: #008056" href="somelink">Name</a>
<b>;</b>
</div>

My question is, how can I read all semicolons in this div using jquery?

+4
source share
2 answers

To include style attributes, try the following:

  alert($('#link1').html().split(";").length - 1);

And get without one, which is in the attributes:

  alert($('#link1').html().replace(/(<([^>]+)>)/ig,"").split(";").length - 1);

Working demo

Update: To find one of the elements ;in and delete it:

$('#link1 a').each(function(){
 if($(this).html().split(";").length==2){
  $(this).html($(this).html().replace(";",""))
}});

Working demo

+4
source

I do not know what you are doing, but if you need it, you can do:

$('#link1').html().match(/;/g).length

/;/g is a regex that matches all ;

+5
source

Source: https://habr.com/ru/post/1536417/


All Articles