Scoping JavaScript: How to operate only on specific elements?
P粉884548619
P粉884548619 2023-09-08 20:36:06
0
1
494

I have a very simple JS code that only does one thing, which is to toggle anopenclass on a specific element on the page. The problem is, I have 4 duplicate.clickSlideelements and.sub_menuelements, and when I click on one of them to trigger the tag, all the elements getopenkind. Only one of the focused elements should get theopenclass.

My best guess is that somethingthisis missing in the JS. But I'm open to solutions to this problem!

jQuery(document).ready(function($) { $(".clickSlide").click(function() { $(".sub_menu").toggleClass("open"); }); });
 

P粉884548619
P粉884548619

reply all (1)
P粉431220279

So the solution (based on Anass Kartit's answer) is this:

jQuery(document).ready(function($) { $(".clickSlide").click(function(){ $(this).children(".sub_menu").toggleClass("open"); }); });
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!