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:
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">