Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to count characters containg an html object?

I’m not able to figure out how to count all the characters of an html string using jquery:

For example:

<link rel="stylesheet" type="text/css" href="style.css" id="basic">

I wanna do something like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var object = $('#basic');
var characters = object.numberOfCharacters;

The answer should be = 67

>Solution :

You can get the ‘outer’ HTML property to get the tag/element selected and then count the length of that element.

See sample code below

$(function() {
  let linkObj = $('#basic');
  console.log(linkObj.prop('outerHTML').length);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<link rel="stylesheet" type="text/css" href="style.css" id="basic">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading