Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery function when calling CSS & HTML

I am confused regarding the use of ' ' and '.' in a jQuery function. When exactly do you use one or the other?

For example,

var main = function(){
    $('.article').click(function(){
        $('.article').removeClass('current')
        $('.description').hide();  
        $(this).addClass('current');
        $(this).children('.description').show();
    }
)};

$(document).ready(main);

Why is it correct to use .addClass('current') and not .addClass('.current'), or children('.description') instead of children('description')?

Thank you, I couldn't really find the answer or knew how to look for it on Google.

like image 618
Cesar Avatar asked Jan 28 '26 05:01

Cesar


1 Answers

The . is when you are referring to a Class. Check this for more about classes. So in your case you are using . when you are doing something with the classes. Example $(this).children('.description').show();. Somewhere in your HTML code there is an element with class .description ( example <div class="description"> </div>). And you didn't use . in .addClass() method because you are not referring to existing class but you are "creating" one.

You should also check this to know more about jQuery selectors..

like image 94
Luka Krajnc Avatar answered Jan 30 '26 18:01

Luka Krajnc