Using code to click on mobile menu

Using code to click on mobile menu

I am using this code to work with mobile menu. 

  1. $('ul.mobile_menu > li > a.hasChildren').not('.active').click(function () {
  2. if ($(this).attr('class') != 'active') {
  3. $('ul.mobile_menu li ul').slideUp();
  4. $(this).next().slideToggle();
  5. $('ul.mobile_menu li a').removeClass('active');
  6. $(this).addClass('active');
  7. }
This works on the root and first level menu items. I am having problem with second and third level menu ites with mobile device.

Any suggestion or can you post the fix.