Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
Javascript Conditional Statement Based on Cookie Value

I'm using cookies to scroll to different sections of a given page. The cookies are saved fine. Only one cookie can exist at a given time. I can see them in the application as they should be. However, there is an issue with my conditionals where only the last if statement runs.

jQuery(document).ready(function ($) {
 
  var internetcsomagcookie = null;
  var csomagcookie = "true";
  var triocsomagcookie = null;
  var tvcsomagcookie = null;


  if ((csomagcookie = "true")) {
      document.getElementById("csomag").scrollIntoView();
  }

  if ((triocsomagcookie = "true")) {
      document.getElementById("trio").scrollIntoView();
  }

  if ((internetcsomagcookie = "true")) {                                 document.getElementById("internet").scrollIntoView();
    
  }

  if ((tvcsomagcookie = "true")) {

      document.getElementById("tv").scrollIntoView();
    
  }
});
#internet,#trio,#tv,#csomag {height:1000px;}


#internet {background:blue;}

#tv {background:red;}

#csomag {background:green;}

#trio {background:orange;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="csomag"></div>

<div id="trio"></div>

<div id="internet"></div>

<div id="tv"></div>

The issue is, that after creating any of the cookies (except for the last one - tvcsomagcookie), whenever I load the page where I want the scrolltoview to happen on, it jumps to the last ID in the code (basically the last if statement executes that scrolls to #tv), despite the tvcsomagcookie returning 'null'. So it runs when it should not.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If statements were originally posted with = (which is for assignment) instead of == for comparison. Updating this inside the code fixed the issue.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!