I have scanned stackoverflow all over for a solution regarding my query and resulted in posting a question. Please could someone assist me, my skill sets sit in PHP and I am not 100% fluent in JS/jQuery.
My code below allows for the div text to be shorted if more than 8 characters.
<div id="theText">Very long text here</div>
<script>
function cutString(id){
var text = document.getElementById(id).innerHTML;
var charsToCutTo = 8;
if(text.length>charsToCutTo){
var strShort = "";
for(i = 0; i < charsToCutTo; i++){
strShort += text[i];
}
document.getElementById(id).title = "text";
document.getElementById(id).innerHTML = strShort + "...";
}
};
cutString('theText');
</script>
However I am now trying to do this on multiple Div as per the below code, I think the method I should be using is arrays, below is a none working version but the concept where I am looking for guidance.
Any help is appreciated.
<div id="theText[]">Very long text here</div>
<div id="theText[]">Even more long text here</div>
<script>
function cutString(id){
var text = document.getElementById(id).innerHTML;
var charsToCutTo = 8;
if(text.length>charsToCutTo){
var strShort = "";
for(i = 0; i < charsToCutTo; i++){
strShort += text[i];
}
document.getElementById(id).title = "text";
document.getElementById(id).innerHTML = strShort + "...";
}
};
cutString('theText[]');
</script>