tags:

views:

39

answers:

3
$(if($('#errorExplanation').length > 0)){
  $('#venue_details').toggle($('#errorExplanation').length > 0); //if there is at least one errorExplanation element on the page, 
  $("#venue_details").load("/load_events/"+ escape($('#request_artist').val()), successCallback );
    }

It seems like i am repeating myself with this code. I basically need to show #venue_details and run the load whenever #errorExplanation').length > 0...is there a better way or is my syntax off

+2  A: 

I think, what you want is this:

if($('#errorExplanation').length > 0){
    var url = "/load_events/"+ escape($('#request_artist').val());
    $("#venue_details").load(url, function() {
        $(this).show();
        successCallback(); );    
    });
}

Not sure what you wanted to accomplish with $(if(...)), but toggle() does not take a boolean value as parameter.

Note that I rearranged the code, so that #venue_details is only shown after the content is loaded.

Felix Kling
A: 
  $('#venue_details').toggle($('#errorExplanation').length > 0); //if there is at least one errorExplanation element on the page, 

You are feeding a boolean, you don't need to feed anything to toggle.

meder
+1  A: 
$(function(){
     if($('#errorExplanation').length >0){

         $("#venue_details").load("/load_events/"+ escape($('#request_artist').val()), function(){
             successCallback();
             $(this).fadeIn();
         );
     }
});
From.ME.to.YOU