Jquery does not run because of syntax

Asked

Viewed 149 times

1

I analyzed my syntax and at first you’re right:

 $(document).ready(function(){

    $('input[placeholder], textarea[placeholder]').focus(function(){
    if($(this).val()==$(this).attr('placeholder'))
        $(this).val('');
    }).blur(function(){
    if($(this).val()=='')
        $(this).val($(this).attr('placeholder'));
    });


    $( ".soluctionsBenefits" ).click(function() {     
        $( ".soluctionsBenefitsShow" ).show();
        $( ".soluctionsDescriptionShow" ).hide();
        $( ".soluctionsApplicationsShow" ).hide();
    });
}

However, points error in the console:

Erro na consola do navegador

Which indicates the following line in my code:

Indicação do erro no meu código

2 answers

3


If you observe the error:

Syntaxerror: Missing ) after argument list

That translates:

Syntax error: Missing ) after the list of arguments

Ability to understand, with the help of indicating the line where the error occurred, that the parentheses of the method have to be closed .ready(), which takes as an argument a function to be executed after the DOM is available.

Rectified code

$(document).ready(function(){

    $('input[placeholder], textarea[placeholder]').focus(function(){
    if($(this).val()==$(this).attr('placeholder'))
        $(this).val('');
    }).blur(function(){
    if($(this).val()=='')
        $(this).val($(this).attr('placeholder'));
    });

    $( ".soluctionsBenefits" ).click(function() {     
        $( ".soluctionsBenefitsShow" ).show();
        $( ".soluctionsDescriptionShow" ).hide();
        $( ".soluctionsApplicationsShow" ).hide();
    });
}); // Faltava fechar os parênteses aqui

Optimizing

Taking advantage of the answer, there is a tip to minimize the interaction with the DOM since you make several calls to the elements in the methods .focus() and .blur():

$(document).ready(function(){

  $('input[placeholder], textarea[placeholder]').focus(function(){

    // colocar elemento em cache para evitar andar sempre a procurar o mesmo
    var $this = $( this );  

    if ( $this.val()==$this.attr('placeholder') )
      $this.val('');

    }).blur(function(){

      var $this = $( this ); 

      if ( $this.val()=='' )
        $this.val( $this.attr('placeholder') );
    });

    $( ".soluctionsBenefits" ).click(function() {

      $( ".soluctionsBenefitsShow" ).show();

      // esconder vários elementos ao mesmo tempo
      $( ".soluctionsDescriptionShow, .soluctionsApplicationsShow" ).hide(); 
    });
});

Essentially, we improve two things:

  1. Avoid always searching for the element in .focus() or .blur() caching:

    var $this = $(this);
    
  2. Hide multiple elements at once avoiding multiple lines of code for the same purpose:

    $( ".soluctionsDescriptionShow, .soluctionsApplicationsShow" ).hide();
    
  • Thank you very much! Hours in front of the computer generate these carelessness, eheh.

2

It’s because there really is one missing ), which is at the end to close the clause of document.ready:

 $(document).ready(function(){

    $('input[placeholder], textarea[placeholder]').focus(function(){
    if($(this).val()==$(this).attr('placeholder'))
        $(this).val('');
    }).blur(function(){
    if($(this).val()=='')
        $(this).val($(this).attr('placeholder'));
    });

    $( ".soluctionsBenefits" ).click(function() {     
        $( ".soluctionsBenefitsShow" ).show();
        $( ".soluctionsDescriptionShow" ).hide();
        $( ".soluctionsApplicationsShow" ).hide();
    });
}) // <---- AQUI

Browser other questions tagged

You are not signed in. Login or sign up in order to post.