0

We need to disable and re-enable with a click, a function which shows us the amount of characters within plenty of < pre >.

The function we use is this:

<script type="text/javascript">
$(window).load(function(){
    $(document).ready(function(){
    $('pre').each(function(i){
        var iCharacters = $(this).text().length;
            $(this).prepend("<b style='color:red'>" + iCharacters + " </b>");
        });
    });
});
</script>

Thanks a lot for your help in advance.

4
  • What do you want to enable with a click? Commented Sep 17, 2018 at 16:42
  • 5
    First you do not need both $(window).load and $(document).ready , secondly your question is not clear Commented Sep 17, 2018 at 16:43
  • I'm sorry for not been clear enough. This function shows the number of character between a <pre> and a </pre>. It sows this characters in red as prepend to the text of these <pre>. We want to be able to toggle between having or not these red numbers while using our documents, mainly to first choose the appropriate one, and then copy it and paste it easily without these numbers. Commented Sep 17, 2018 at 16:51
  • I thought about toggle between this code, and other doing nothing, but I believe that is not correct. Commented Sep 17, 2018 at 16:53

1 Answer 1

2

I believe an easy way to do this would be to always display the numbers but they hide or show them on the click of a button using jQuery's toggle method. You can find a working example here: https://jsbin.com/pohagaz/1/edit?html,js,output.

$("pre").each(function(i){
  var iCharacters = $(this).text().length;
  $(this).prepend("<b class='charCount' style='color:red'>" + iCharacters + " </b>");
});

$("#showCounts").click(function() {
  $(".charCount").toggle();
});
Sign up to request clarification or add additional context in comments.

1 Comment

WOW! Joel Lord! This is exactly what we need! You really made my day! THANK YOU VERY MUCH! This was my first Question! How do I do to prize your Answer???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.