.length locks all code

1

I need to know if the user is logged in using Javascript (jQuery) to run some commands (purely aesthetic). The fastest alternative I found was creating a hidden input that is only placed on the page if the user is logged in (verified through PHP) and check by jQuery if that input exists. But when I put the code below inside a $(document).ready() along with other functions, none of them work anymore, all my jQuery goes to the bag. Here is the code:

$(document).ready(function()
{
   .
   .
   .
   if ($("#estoulogado").length)
   {
      $("#email").prop('disabled', true);
      if ($("#estoulogado").attr('faoulo') == "fabrica")
      {
        $("#lojas").prop('disabled', true);
        $("#selectdepartamentos").prop('disabled', true);
        $("#nome").prop('disabled', true);
      };
      else if ($("#estoulogado").attr('faoulo') == "loja")
      {
        $("#fabrica").prop('disabled', true);
        $("#selectlojas").prop('disabled', true);
      };
   };
   .
   .
   .
});

Opening the console (as suggested by Renan) I encountered a syntax error on this line:

$("#email").prop('disabled', true);

The error is as follows: Uncaught SyntaxError: Unexpected token (

No matter what I put inside if , all my code hangs, it does not work. Why does this occur? I also accept a better idea than this one to check if the user is logged in.

    
asked by anonymous 14.11.2014 / 18:00

3 answers

1

I found the error. I am posting as an answer because I asked the question as a guest and when completing my registration I can no longer comment on the question because it is no longer my own.

I wanted to thank Renan for helping me, if it was not for him I do not think I would have found the error, because I still think the problem was .length . But since I am a new user and the question no longer belongs to me, I can not give it a reputation.

So let's go to the error.

The problem was in the semicolon after the keys of if within if . Since there is else after it, there could not be a semicolon there.

Thanks for the help people.

    
14.11.2014 / 19:32
3

The error Unexpected token ( means that there is a pair of badly enclosed parentheses in the code. Two considerations:

  • It was initially stated in the question that a parenthesis was missing from the code. You have corrected it but if you still get the same error it is because there are more pairs of badly enclosed parentheses. This is in the code you did not show;

  • The error does not occur in the statement that the console tells you. $("#email").prop('disabled', true); is syntactically correct.

Some suggestions:

  • Make the most of your IDE. Most mark syntax errors visually for ease of correction;

  • From most nested to outermost expressions, delete pairs of parentheses with everything inside them. In the end it ends up leaving what has not been closed.

Good luck!

    
14.11.2014 / 18:47
1

Missing ) at the end of the code

Do this way

$(document).ready(function()
{
   .
   .
   .
   if ( $("#estoulogado").length )
   {
   .
   .
   .
   }
   .
   .
   .
});
    
14.11.2014 / 18:09