@phani posted a new topic, subject "My jquery from external .js is not working?". It read:

   my code is
    [code] 
            <html>
            <head>
            <title></title>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
            <script src="phc.js"></script>
            <style>
            .effect
            {
            color:red;
            }
            </style>
            </head>
            <body>
            <button class="click-ctrl">jjasdnfjknfjdndfjndsnnakj</button>
            </body>
            </html>
        
    [/code]
    
    my jquery
    [code] 
            $(document).ready(function(){
                $(".click").click(function(){
                    $(this).addClass(".effect");
                });
            });
        
    [/code]

@PaulOB replied

    Hi,
    
    First mistake is you are looking for an element with a class called .click.
    
    You don't have one 
    
    You have an element with a class of .click-ctrl which I guess is the one you meant.
    
    The second error is that you added a dot before the classname in the addClass function. There is no dot required there because as you are adding a class jquery assumes it is a class.
    
    Here is the revised code.
    [code] 
        $(document).ready(function(){
            $(".click-ctrl").click(function(){
                $(this).addClass("effect");
            });
        })
    [/code]

To which @phani replied

    thaks , that helped me.

So @system replied

    This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.


{'source': 'discourse', 'url': 'https://www.sitepoint.com/community/t/my-jquery-from-external-js-is-not-working/231252', 'title': 'My jquery from external .js is not working?', 'views': 1037, 'has_accepted_answer': False, 'word_count': 164, 'like_count': 1, 'reply_count': 1, 'participant_count': 2, 'posts': 4, 'tags': 'jquery', 'author': '@phani', 'participants': '@phani, @PaulOB, @system', 'image_url': [None], 'created_at': '2016-07-22T16:57:09.128Z', 'last_posted_at': '2016-10-22T00:28:21.100Z'}