开发者

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

开发者 https://www.devze.com 2023-01-10 04:38 出处:网络
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.

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

精彩评论

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