NerishaB
asked on
Javascript - Calculting the number of charcters in an SMS
Hi,
I have a page tha allows the user to send SMS's. I also have a function that will count the characters that the user has left, out of 160. When the user reaches "0/160", the message counter displays: "Message 2", ans so on. Now, all of this works fine, except for the fact that the counter seems to to skip the first character, and starts the count only after the second character is entered. Can anyone help?
See my code below:
I have a page tha allows the user to send SMS's. I also have a function that will count the characters that the user has left, out of 160. When the user reaches "0/160", the message counter displays: "Message 2", ans so on. Now, all of this works fine, except for the fact that the counter seems to to skip the first character, and starts the count only after the second character is entered. Can anyone help?
See my code below:
<script language="JavaScript" type="text/javascript">
function limitText(limitField, limitNum){
var count = limitField.value.length;
var messagecount = document.getElementById("messagecount");
var countdown = document.getElementById("countdown");
if (count < limitNum){
countdown.innerHTML = (limitNum - 1) - count;
messagecount.innerHTML = "Message 1";
}
else if (count < limitNum * 2){
countdown.innerHTML = (limitNum * 2) - count;
messagecount.innerHTML = "Message 2";
}
else if (count < limitNum * 3){
countdown.innerHTML = (limitNum * 3) - count;
messagecount.innerHTML = "Message 3";
}
else if (count < limitNum * 4){
countdown.innerHTML = (limitNum * 4) - count;
messagecount.innerHTML = "Message 4";
}
else {
limitField.value = limitField.value.substring(0, limitNum * 4);
}
}
<th> Message</th>
<td>
<p><span id="header"><textarea style="WIDTH: 489px; HEIGHT: 36px" id="MessagesMessageHeader" cols="44" name="{MessageHeader_Name}">{MessageHeader}</textarea> </span><br>
<br>
<textarea style="WIDTH: 488px; HEIGHT: 63px" onkeydown="limitText(this.form.MessagesMessageBody, 161);" id="MessagesMessageBody" rows="4" cols="46" name="{MessageBody_Name}">{MessageBody}</textarea> <br>
<div id="sms">
<span style="FONT-WEIGHT: bold" id="countdown">160</span> / 160 characters<br>
<span id="messagecount"> </span>
<p></p>
</div>
<span id="footer"><textarea style="WIDTH: 489px; HEIGHT: 44px" id="MessagesMessageFooter" rows="1" cols="43" name="{MessageFooter_Name}">{MessageFooter}</textarea></span></td>
</tr>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER