PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 8, 2022

[FIXED] How can i make a loop with jquery?

 November 08, 2022     css, javascript, jquery, loops, menu     No comments   

Issue

I made this in jquery, but it just works one time. How can i make a loop and fix it?

$(function() {

    $('.bar-menu').on().click(function() {
        $('.header-menu-mobile ul').css('display', 'block');
        $('.form_mobile .search-bar input').css('margin-top', '-297px');
        $('.header_mobile .form_mobile .icon-search').css('margin-top', '-283px');
        $('.header_mobile .form_mobile .bar-menu').css('margin-top', '-295px');
        $(this).off().click(function() {
            $('.header-menu-mobile ul').css('display', 'none');
            $('.form_mobile .search-bar input').css('margin-top', '-72px');
            $('.header_mobile .form_mobile .icon-search').css('margin-top', '-59px');
            $('.header_mobile .form_mobile .bar-menu').css('margin-top', '-67px');
        });
    });
});

Solution

off() will remove the on() event handler. Use flag variable instead of it.

$(function() {
    $('.bar-menu').on().click(function() {
        $(this).context.flag = !$(this).context.flag;
        if($(this).context.flag) {
            $('.header-menu-mobile ul').css('display', 'block');
            $('.form_mobile .search-bar input').css('margin-top', '-297px');
            $('.header_mobile .form_mobile .icon-search').css('margin-top', '-283px');
            $('.header_mobile .form_mobile .bar-menu').css('margin-top', '-295px');
        } else {
            $('.header-menu-mobile ul').css('display', 'none');
            $('.form_mobile .search-bar input').css('margin-top', '-72px');
            $('.header_mobile .form_mobile .icon-search').css('margin-top', '-59px');
            $('.header_mobile .form_mobile .bar-menu').css('margin-top', '-67px');
        }
    });
});


Answered By - pullidea-dev
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing