开发者

jquery how to retrieve the number of characters in a p tag?

my problem is very simple: I want to know how many characters are inside a 'p' tag and if the number is over 100 fire an event on hover. I tried to retrieve the char number like this:

var charLength = $('.myPar').val().length;
<p class="myPar">this is the content of the paragraph</p>

it returns always zero as value and it seems t开发者_如何学JAVAo work only for textarea or input tags. Any idea?

Thanks in advance!

Mauro


$('.myPar:first').html().length or $('.myPar:first').text().length depending on your use case


replace .val() with .text()

var charLength = $('.myPar').text().length;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜