Problems with Variables in Jquery
Hello, I am starting with jQuery and I have a problem declaring variables. I am making a script to count the number of characters inside an html element, when I make like this, it works: <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#demo").text($("#mydiv p").text().length); }); </script> </head> <body> <div id="mydiv"> <p>Hello World!!</p> </div> <p id="demo"></p> </body> </html> However, when I try to put the counting inside of variable, it doesn't work anymore. I tried to make a script like this: <script> var count = $("#mydiv p").text(); $(document).ready(function(){ $("#demo").text(count.length); }); </script> and like this: <script> var count = $("#mydiv p").text().length; $(document).ready(function(){ $("#demo").text; }); </script> And noting works... I believe that is a very silly question, but hopefully somebody can make me understand where it is my mistake. Thanks for the help