javascript - Count length of each class -
i have below code in this fiddle.
html
<span class="myname">john smith</span> <span class="myname">john smith</span> <span class="myname">john smith</span> <span class="myname">john smith</span> <span class="myname">john smith</span> <span class="myname">john smith</span>
javascript
$('.myname').each(function(){ var text = $(this).html().split(' '), len = text.length, test = $(this).val().length, result = []; console.log('outside: ' + test); for( var = 0; < len; i++ ) { result[i] = '<span class="name-'+[i]+'" >' + text[i] + '</span>'; console.log('inside: ' + test); } $(this).html(result.join(' ')); console.log('after: ' + test); });
it adds span each word in myname div. want count charecters in each div if long name can cut first name down j smith example.
but if @ console log have tried count length in 3 different places , comes 0? doing wrong?
zerkms right. want change this:
test = $(this).val().length,
to this:
test = $(this).text().length,
Comments
Post a Comment