Sunday 18 August 2019

How to limit the length of text in a paragraph

<html>
 <head>
 </head>
    <body>

<p>Breaking India: Western Interventions in Dravidian and Dalit Faultlines is a book written by Rajiv Malhotra and Aravindan Neelakandan which argues that India's integrity is being undermined.</p>

    </body>

<script>
function limitText(selector, maxLength) {
    var element = document.querySelector(selector),
        limit = element.innerText;

    if (limit.length > maxLength) {
        limit = limit.substr(0,maxLength) + '...';
    }
    return limit;
}

document.querySelector('p').innerText = limitText('p', 59);
</script>

</html>