lundi 25 juillet 2016

Highlight with color all words which starts with uppercase letter using Javascript

I'm newbie in Javascript and jQuery. I have a text inside a paragraph. I want to highlight with yellowgreen color all words which starts with uppercase letter. Here is my source code. But it does not works properly.

    <!DOCTYPE html>
    <html>
    <head>
<style>
.mycls {background-color:yellowgreen}
</style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        var str = $(p).text();

                words = str.split(' ');

                for (var i = 0; i < words.length; i++) {
                    var w = words[i].split('');

                    if (w.charAt(0) === w.charAt(0).toUpperCase()) {
                        $(this).addClass("mycls");
                    }

                 //   words[i] = letters.join('');
                }
    });
    </script>
    </head>
    <body>

    <p>President of USA Barack Obama is ...</p>

    </body>
    </html>

Thank you to all!

Aucun commentaire:

Enregistrer un commentaire