.length locks all the code

Asked

Viewed 71 times

1

I need to know if the user is logged in through Javascript (jQuery) to run some commands (purely aesthetic). The quickest alternative I could find was to create 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 within a $(document).ready() along with other functions, none of them works anymore, all my jQuery goes to bag. Follows 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 found 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 the if, all my code hangs, it doesn’t work. Why does this happen? I also accept a better idea than this to check if the user is logged in.

  • 2

    There’s one missing ) at the end of the code... shall be });. Is that your problem?

  • 2

    probably a syntax error. open the console and check

  • Ah, I forgot to put the ) in the code posted here, I’ll edit, but it’s right in the actual code.

  • If the console indicates an error, add this information to the question. Any error in Javascript causes all scripts to stop after the error in most browsers.

  • My code all runs normal, but the time I went to add this part, EVERYTHING stopped working.

  • @Rafael doesn’t cost more than five seconds of your time to press F12 and see if there’s an error in the console. If there is no error in the console, enter this in the question as well, as this is important to determine the cause of the problem.

  • Sorry Renan, I guess you thought I didn’t want to look at the console. When you sent the comment I was already looking at the console and editing the question. I edited the question and put the full code that is inside the if. Thanks Renan

  • @Rafael no problem. It’s a syntax error, okay? Some pair of parentheses is not closed properly in your code, and it is not in the statement where the error is shown.

Show 3 more comments

3 answers

3

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

  • It was initially indicated in the question that a parenthesis was missing from the code. You fixed it, but if you still get the same error it’s because there are more pairs of poorly closed parentheses. This is in the code that you didn’t show;

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

Some suggestions:

  • Get more out of your IDE. Most mark syntax errors visually to make it easier to fix;

  • From the most nested expressions to the most external ones, delete pairs of parentheses with whatever is inside them. In the end it ends up leaving what was not closed.

Good luck!

1

We’re missing a ) at the end of the code

Do so

$(document).ready(function()
{
   .
   .
   .
   if ( $("#estoulogado").length )
   {
   .
   .
   .
   }
   .
   .
   .
});
  • I was just wrong in the code posted, the real code is right, I’ve edited, thank you.

1


I found the mistake. 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 no longer appears as mine.

Before I wanted to thank Renan for helping me, if it were not for him I think I would not have found the mistake, because I would still be thinking that the problem was the .length. But since I’m a new user and the question no longer belongs to me, I can’t give you reputation.

So let’s make a mistake.

The problem was in the semicolon after the keys of the if within the if. How there is a else after it, there couldn’t be a semicolon there.

Thanks for the help guys.

Browser other questions tagged

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