0

Hi I have a site where dynamically load page inside the conatiner. I have a menu of navigation like this:

<div class="menu">
    <p align="left" id="home" class="titolo_menu">home</p>
    <p align="left" id="azienda" class="titolo_menu azienda"><br>azienda</p>
    <p align="left" id="staff" class="titolo_sotto_menu azienda">staff</p>
    <p align="left" id="risorseumane" class="titolo_sotto_menu azienda">risorse umane</p>
</div>

And into my jQuery I have added this line:

$(document).ready(function(){
    $('.titolo_menu').bind('click',function(){
            $('.titolo_menu').unbind('click');
             //more code...
     });
  });

I want when a user click on a p with class "titolo_menu" disable the event click but doesn't work, I can click more and more time. How can I solve it?

10
  • 2
    Your code works for me? jsfiddle.net/pimlinders/j8cDC Commented Oct 31, 2012 at 15:07
  • align attribute is deprecated, use <ul> + <li> inside a <nav> element for navigation, and use <a>'s for links. Then reproduce your problem on a fiddle so it's easier to help you ^^ Commented Oct 31, 2012 at 15:07
  • @undefined because after I have to enable again the click event Commented Oct 31, 2012 at 15:09
  • 2
    As of jQuery 1.7, bind/unbind is deprecated in favor of the on/off methods. Commented Oct 31, 2012 at 15:09
  • @AlessandroMinoccheri What are you trying to achieve? Commented Oct 31, 2012 at 15:09

4 Answers 4

2

Though I couldn't figure out what you really want in your application, consider using data-* attributes to same some state inside DOM itself, like:

$(document).ready(function(){
    $('.titolo_menu').on('click',function(){

        // let's create a reference for current element
        $p = $(this);

        // if clicked element had data-disabled attribute set, return
        if($p.data('disabled'))
            return;

        // disable element;
        $p.data('disabled', true);

        // do your stuff here, e.g.
        $.get('dapage.html', function(data){

            // do your stuff on data retrieving
            $(something).html(data);

            // re-enable element
            $p.data('disabled', false);

        });

   });
});

This way you can have control over events, without messing with unbinding.

Sign up to request clarification or add additional context in comments.

1 Comment

This work I have data attributes and now I can disable and enable easily my DOM elements
0

You can try this....

$(document).ready(function(){
var return_flag = true;
    $('.titolo_menu').bind('click',function(){
        if(return_flag){
             //more code...
        }
    return_flag = false;
     });
  });

Comments

0

Just change class name

$('.titolo_menu').bind('click',function(){
           $(this).removeClass('.titolo_menu');
             //more code...
     });

Or if you want to disable all elements:

$('.titolo_menu').bind('click',function(){
            $('.titolo_menu').removeClass('.titolo_menu');
             //more code...
     });

Doesn't work if you use 'live' insted of 'bind'

//Edit Sorry it won't work. Just use live:

$('.titolo_menu').live('click', function(){
  $('.titolo_menu').removeClass('.titolo_menu');
}); 

But if you just want to prevent multiple click whie page is loading just use

.attr('disabled', 'disabled');

1 Comment

It's actually quite the opposite, it will only work IF you use .live instead of .bind.
0

Firstly I'd like to advice you to use an unsorted list when creating menu's and use <a> tags to link the pages.

Example:

<ul id="menu">
    <li id="home" class="titolo_menu"><a href="#home">home</a></li>
    <li id="azienda" class="titolo_menu azienda"><a href="#azienda">azienda</a></li>
    <li id="staff" class="titolo_sotto_menu azienda"><a href="#staff">staff</a></li>
    <li id="risorseumane" class="titolo_sotto_menu azienda"><a href="#risorseumane">risorse umane</a></li>
</ul>

In jQuery you could then do something like this:

$(function() {

    $("#menu a").click(function(e) {
        var clickedElement = $(this);
        var clickedElementText = clickedElement.text();

        // Check if current link is not yet active
        if (!$(clickedElement).hasClass("active")) {

            if ($(".active").length > 0) {
                // Deactivate current active link
                var active = $(".active ");
                var activeText = $(active).text();

                $(".active").replaceWith("<a href = '#" + active.parent().attr("id") + "' > " + activeText + " </a>");
            }
            // Change the clicked link to be activated
            $(clickedElement).replaceWith("<span class='active'>" + clickedElementText + "</span>");

            // Load your page or whatever here :)

            // Prevent normal link behavior
            e.preventDefault();
        }
    });
});

5 Comments

I can't use tag a because I have to do particular thing and a link isn't the right solution, I know well html and I know that is better but not in this case sorry
Could you explain why it isn't a correct solution. My solution should work if you change them to <p> tags.
because I want that when a use go over doens't go lo a link there a re case where isn't clickable like if you are in the same page doesn't have to refresh the page and is more comfortable to use p instead f tag a
Still not entirely sure what you're trying to achieve. Either way, I hope someone is able to answer your question.
already answer, is a very complex layout and logic is the best way for many reason believe me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.