Lets assume you can never know what string will be passed to the browser, any combination of any characters of any length and I want to limit this to lets say.. 50 chars.
This is what I have at the moment:
<script type="text/javascript">
var x = "Hello this sentance was created in the browser!"
for(i=0; i<x.length; i++){
if(i == 50){
}
}
</script>
Eventually x will be something like:
var x = $('#textBit').html();
How will I remove everything in the array (string) after position 50, will I need a new for loop kinda like this (it may be wrong just thinking it up) pseudo code:
loop remainder string{
do until end of array{
remove item
}
}
Or is there a better way of doing it? Thanks.
Just use
substr()(MDN docu). This will return the substring meeting you criteria, e.g., beginning at the start and having at most 50 characters: