tags:

views:

118

answers:

2

Hello,

I have what seems to be a simple issue. I have a jquery function that's running on page load, despite the fact that I specifically set it to run after the 'click' event on a specific element.

This is the function:

    $('#boxShow').click(function() {
      $('#colorBox').animate({
        height: 'toggle'
      }, 400, function() {
        // Code that will run after the click
      });
    });

Is there a way to prevent this code from running before the 'click' event? Thanks!

EDIT:

The full jquery code:

$(document).ready(function() {  

    $("#accordion").accordion({
        event: "click"
    });

    $("#TagsSelectBox").multiSelect({ 
        minWidth:130,
        selectedList:5,
        showHeader:false
    });

    $('#boxShow').click(function() {
        $('#colorBox').animate({
        height: 'toggle'
        }, 400, function() {
            // Animation complete.
        });
    });

    $('#test').colorPicker({            
        defaultColor: 0, // index of the default color (optional)
        columns: 13,     // number of columns (optional)  
        click:function(c){
        $('#boxShow').css("background-color",c);
      } 
    });
});

EDIT: Just in case anyone wants to know, here's the final version after making a few adjustments to make it generic enough for multiple 'color-pickable' items:

Final Version

If you want to try it out yourselves, you can download the color picker plugin from the syronex site. The link is in the comments!

Thanks to everyone who helped me achieve this!

A: 

Guessing that you mean that the query is not getting any elements, and therefore not working.

You need to put it inside a document ready handler to call it after the document loads.

$(document).ready(function(){
    $('#boxShow').click(function() {
      $('#colorBox').animate({
        height: 'toggle'
      }, 400, function() {
        // Code that will run after the click
      });
    });
});
digitalFresh
That's not what he seems to mean. It seems that the handler is running without his clicking on the element.
DLH
This is the only reason I can think of. I will edit this if there is any info that suggests another problem.
digitalFresh
I edited my question with the full jQuery code. Thanks!
Rafa
The problem doesn't seem to be related to the $(document).ready statement, since it was already there. Just in case I wasn't clear enough, the code that is running is $('#colorBox').animate() and everything within it.
Rafa
+2  A: 

If I had to guess I'm not sure of what your colorpicker plugin is, so I would say try commenting out the lines that say click on that control, because it might initiate a click to that control

$('#test').colorPicker({             
    defaultColor: 0, // index of the default color (optional) 
    columns: 13,     // number of columns (optional)   
    //click:function(c){ 
    //$('#boxShow').css("background-color",c); 
  //}  
}); 

Since it seems as if all your tags are divs, try changing the click event function to this

$('div').click(function(e) { 
  var $target = $(e.target); 

  if ($target.is('#boxShow')) { 
       $('#colorBox').animate({ 
        height: 'toggle' 
        }, 400, function() { 
        // Code that will run after the click 
      }); 

  } else { 
    // do actions for a click anywhere else inside div
    e.preventDefault(); 
  } 
}); 

If that works then something else is wrong with your code that you have posted.

Jake
Thanks. That was a good idea actually, but it didn't do the trick. $('#colorBox').animate({...}) still runs even though I commented out those 3 lines with the click event from the color picker. I'm going to provide the full HTML code for this, just a minute.
Rafa
I just edited my post with the full code. check it out.
Rafa
Thank you so much, Jake. Your code solves my problem! But I'd like to understand why it works, would you kindly explain? I thought the click event alone was supposed block something from happening up until the click.
Rafa
@Rafa- As I said there is something else in your code triggering that click event( your problem exist somewhere in your code), so my code make sure that the actual event triggered event is a clicked event and that it **matches** that specific div **id**.
Jake
Also since you're new to the site, dont forget to mark the correct answer to your solution
Jake
hmm, I wonder what it could be.. everything necessary to reproduce the problem I had was on that 'Full Code' link. I hope someone can spot what I did wrong. Any suggestions on how I can debug this? / Ok jake, I'll mark your answer, thanks for the heads up.
Rafa
**firebug** can help debug it
Jake
Thanks Jake! I posted the final result at the end of the question. It turned out it actually was the plugin's fault. Somehow it activated the click event without any actual click... But I worked around this issue, hehe.
Rafa