vulkan vulkan - 7 months ago 101
HTML Question

jquery textarea inside html tag strip/remove

I want to count characters in textarea but i dont want it to count html tags.

Snippet :



$('#m-summary').keyup(function() {
var sumTxt=$('#m-summary').val().length;
if(sumTxt < 200) {
$('#result-summary').html("<span>200 / </span><span style='font- style:normal;font-weight:bold;'>"+sumTxt+"</span>");
}
else {
$('#result-summary').html("<span style='color:green'>200 / </span><span style='color:green;font-style:normal;font- weight:bold;'>"+sumTxt+"</span>");
}
}
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="text-area">
<textarea name="m_summary" id="m-summary"></textarea>
<div id="result-summary">200 / 0</div>
</div>





I write inside textarea : hello . count length = 12.

Answer

Use : str.replace(/<\/?[^>]+(>|$)/g, "")

Snippet:

$('#m-summary').keyup(function() {
  var sT=$('#m-summary').val().replace(/<\/?[^>]+(>|$)/g, "");
  var sumTxt=sT.length;
  if(sumTxt < 200) {
    $('#result-summary').html("<span>200 / </span><span style='font- style:normal;font-weight:bold;'>"+sumTxt+"</span>");
  }
  else {
    $('#result-summary').html("<span style='color:green'>200 / </span><span style='color:green;font-style:normal;font-  weight:bold;'>"+sumTxt+"</span>");
  }
}
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="text-area">
  <textarea name="m_summary" id="m-summary"></textarea>
  <div id="result-summary">200 / 0</div>
</div>