Reputation: 1522
My textarea can count string/character when user enter input. Here is my jsfiddle.
My problem is I want set condition mean when string/character is 158 above, I want show message inside <div id="message">We will deduct 2 credit from your account</div>
and same when string/character is 316 above the message will update and show<div id="message">We will deduct 3 credit from your account</div>
and continue ..
Example:
158 *2
158 = We will deduct 2 credit from your account
316 = We will deduct 3 credit from your account
474 = We will deduct 4 credit from your account
632 = We will deduct 5 credit from your account
...
Upvotes: 0
Views: 1165
Reputation: 2005
$('#myInput').keyup(function() {
$('#charCount').text(this.value.length);
var length = this.value.length;
if (length % 158 == 0) {
$('#message').html('We will deduct '+ (length/158) +
' credit from your account;' )
}
});
Upvotes: 0
Reputation: 82241
on textarea keyup you can check no. of characters presents and accordingly you can write if-else conditions to display the message.
check this:working fiddle
$('#myInput').keyup(function() {
if(this.value.length<=158)
$('#charCount').text('We will deduct 1 credit from your account');
else if(this.value.length>158 || this.value.length<316)
$('#charCount').text('We will deduct 2 credit from your account');
else if(this.value.length>316|| this.value.length<316)
$('#charCount').text('We will deduct 3 credit from your account');
});
Upvotes: 0
Reputation: 55750
Try this
$('#myInput').keyup(function() {
var len = this.value.length ;
$('#charCount').text(len);
var creditCount = 158;
if( len > creditCount ){
var credits = Math.floor(len / creditCount );
$('#message').html('We will deduct '+ credits + ' credit from your account;' )
}
});
Upvotes: 0
Reputation: 665
Try it
$('#myInput').keyup(function() {
$('#charCount').text(this.value.length);
var c = parseInt(this.value.length / 158);
if(c > 0)
$('#message').text('We will deduct '+c+' credit from your account');
});
Upvotes: 2
Reputation: 1994
1) find length of String
2) Divide it by 158
3) Add '1' to the answer
4) Create message using String concatenation - "We will deduct" + output of (3) + "credit from your account"
5) jQuery("#message").html("We will deduct" + output of (3) + "credit from your account");
Upvotes: 0