-2

This might be a rather silly question. But how do you highlight or select any text or p tag in the body and jQuery will count the selected/highlighted characters? Thanks

I've got this, but getting 0

<script>
$(document).ready(function(){

        $('p').mouseup(function(){

        var length = $(this).val().length;

        console.log(length);

    });

});
</script>

P.S. For those who voting down. Why do you do that? Should not we be helping each other?!

medzi
  • 407
  • 1
  • 8
  • 20

2 Answers2

1

This should do the trick

$('.paragraphCount').html('Paragraph count is '+$('p').addClass('highlight').length);

Rachit Doshi
  • 183
  • 1
  • 6
  • Cool, the thing is that I know how to count the value or characters it is just that what (mouse) event I meant to be using to do that? So when you live select a certain text then console will fire the total length of selected text – medzi Jun 17 '13 at 16:25
1

The following JsFiddle Link shows up selected text for all html tags below

http://jsfiddle.net/zKnFs/1/

<input id="inp" value="Hello world!"/>
<textarea id="txtarea">Hello world!</textarea>
<p id="para"> This is a paragraph</p>
<span id="spa"> This is a span</span>
<label id="lbl"> This is a label</label>

Hope this works for you :)

Rachit Doshi
  • 183
  • 1
  • 6