if the checkbox is selected, navigate to the displayed section

In my scenario, I have a total of 5 checkboxes and corresponding to each checkbox, there are 5 div elements. The functionality that I need is that when a checkbox is checked, the associated div should be displayed; otherwise, it should be hidden. The checkboxes are positioned fixed while the divs are part of a scrolling section. Additionally, I am looking to implement a function where when a checkbox is checked, the page automatically scrolls to display the corresponding div.

$('#first').click(function() {
  if ($(this).is(':checked')) {

    $("#fifth").show(400);
  } else {
    $("#fifth").hide(400);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="row">

      <input id="first" type="checkbox" class="styled" checked>
      <label for="checkboxhovuz">
      On check you go to the 5th div
      </label>


<div class="row" >
   <br><br><br><br><br><br>text first<br><br><br>
</div>
<div class="row">
   <br><br><br><br><br><br>text second<br><br><br>
</div>
<div class="row">
   <br><br><br><br><br><br>text third<br><br><br>
</div>
<div class="row">
   <br><br><br><br><br><br>text fourth<br><br><br>
</div>
<div class="row" id="fifth">
   <br><br><br><br><br><br>text fifth<br><br><br>
</div>

Answer №1

One suggestion is to utilize the .animate() method in order to smoothly scroll to the specified element when the conditional statement is true, like so:

$('html, body').animate({
        scrollTop: $("#fifth").offset().top
    }, 2000);

Example of How to Implement this Code Snippet:

$('#first').click(function() {

  if ($(this).is(':checked')) {
  
    $("#fifth").show(400);
    
    $('html, body').animate({
        scrollTop: $("#fifth").offset().top
    }, 2000);
    
  } else {
  
    $("#fifth").hide(400);
    
  }
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="row">

    <input id="first" type="checkbox" class="styled" checked>
    <label for="checkboxhovuz">
      On check you g to 5-th div
      </label>


    <div class="row">
      <br><br><br><br><br><br>text first<br><br><br>
    </div>
    <div class="row">
      <br><br><br><br><br><br>text second<br><br><br>
    </div>
    <div class="row">
      <br><br><br><br><br><br>text third<br><br><br>
    </div>
    <div class="row">
      <br><br><br><br><br><br>text fourth<br><br><br>
    </div>
    <div class="row" id="fifth">
      <br><br><br><br><br><br>text fifth<br><br><br>
    </div>
    <div class="row">
      <br><br><br><br><br><br>text sixth<br><br><br>
    </div>
    <div class="row">
      <br><br><br><br><br><br>text seventh<br><br><br>
    </div>
    <div class="row">
      <br><br><br><br><br><br>text eigth<br><br><br>
    </div>

Answer №2

give this a shot

$(document).ready(function() {
  if ($('#primary').is(':checked')) {
    $("#secondary").show(400);
  } else {
    $("#secondary").hide(400);
  }
});

Answer №3

I needed to include the code

document.getElementById('tulantisular').scrollIntoView();

The complete code snippet is sourced from Scroll / Jump to id without jQuery

$('#first').click(function() {
  if ($(this).is(':checked')) {
    $("#fifth").show();
    document.getElementById('fifth').scrollIntoView();
  } else {
    $("#fifth").hide();
  }
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I need to confirm the validity of minDate and maxDate in a ReactJS component, ensuring that maxDate is always at least 5 years after min

start from today and select a date end 5 years from the starting date I haven't attempted anything yet. Seeking help for a solution. ...

Revamping this snippet - JavaScript with NodeJs

Currently working on a basic CRUD application, encountering an issue with obtaining the auto-incrementing value for the latest account in MongoDB. To provide more context, I've included the snippet below to achieve the following: 1) Conduct validati ...

What is the best way to add an element after a specific child element with jquery?

Is there a way to add a paragraph element after a specific div class element? I am looking to include a p element within the second text div class. I attempted a solution, but it is not producing the desired result. The current solution is not effective. ...

preclude any dates prior to the chosen date

I need a solution for a scenario where I have 5 datepickers in sequence. When I select a date on the first picker, all previous dates should be disabled when selecting on the next picker. Additionally, once a date is selected on one picker, the following ...

The Bootstrap 5 navigation bar does not extend to the full width of its parent

While working on a Bootstrap navigation inside a container, I encountered an issue where the navigation bar was not expanding to match the width of the container. It seemed like the padding had to be manually adjusted to fit properly. Below is the code sn ...

I'm curious if there is a method to incorporate localStorage into the initialState of Redux Toolkit within Next.js 14

Attempting to establish the initial value of a Redux Toolkit slice for dark mode using localStorage is proving problematic in Next.js, as the window object is not defined on the server-side, resulting in errors. The typical workaround involves using if (t ...

What are the steps to integrate my server-side PHP code into this form wizard?

Having created a straightforward 3 step form wizard for registration, I encountered an issue at the last stage when users click on submit. The desired action is to direct users to the PHP code that has been written. Update: Despite updating the HTML code ...

An issue occurred while attempting to access the Stripe checkout page

While working on my e-commerce website using next.js and stripe checkout, I encountered an error during the checkout process. I am utilizing the use-shopping-cart package, but suspect it may be causing the following response error from stripe-checkout: & ...

Inquiring about the best method to determine the intersection point between two lines in Draw i.o

Can someone please explain how to find the coordinate point where two lines intersect in draw i.o? I am looking for a Javascript function to solve this issue and get the coordinate of the point only. For example: ...

Is RaphaelJS truly a vector-based tool?

Is it possible to position a circle at 50% of the width of the canvas using RaphaelJS without having to calculate the exact pixel value? I am looking for a simple way to place an element at half its container's width, but I'm not sure if this can ...

code in html/javascript that is securely implemented for adobe air applications

Creating a desktop application using Adobe Air is my goal, but I have concerns about the security of the HTML/JavaScript source code. My application will feature paid media content, and I am hesitant to expose any URLs associated with it. What methods ca ...

Switching images using jQuery

Issue I'm feeling overwhelmed by the abundance of JavaScript code hints and finding it difficult to determine where to begin. Any assistance would be greatly appreciated. Essentially, I have a primary full-screen background image set in the CSS on t ...

Calculation Error in JavaScript/JQuery

I've been working on a JavaScript function to calculate the sum of values entered into textboxes, but it seems to be giving me inaccurate results in certain cases. Check out the FIDDLE here Enter values : 234.32 and 32.34 Result: 266.6599999999999 ...

Retrieve JSON data from an object using the Amplify Storage feature

I recently retrieved data from an object in an S3 Bucket using Amplify. export function amplify() { let key = "68a92d44-f25a-4bd8-9543-cc95369ae9a0"; return Storage.get(key + ".json", { download: true }) .then(function(result) { return ...

Encountering a NaN result while attempting to incorporate a drop-down menu into a newly developed macro calculator

Before the calculator was functioning smoothly, but as soon as I included the activity level selection from the dropdown menu, things took a turn. The expected output is supposed to be the result of multiplying the user's chosen activity level by the ...

Bootstrap - creating seamless navigation between accordion sections on a single webpage

I am currently attempting to create internal links within an accordion menu on the same page. Here is the code I have been working with: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> & ...

How to display a minimalist white dot in three.js

I am currently working with THREE.WebGLRenderer and I have a requirement to display multiple white dots of the same size at specific locations in 3D space. Is it recommended to use sprites, calculate the 2D screen coordinates, and utilize SpriteMaterial w ...

Is there a way to navigate by scrolling, moving a centrally-positioned SVG along a path while also resizing the SVG?

After following the instructions in this post about resizing SVGs, I managed to keep the red square on the path while resizing the SVG. However, a new issue arises when scrolling down - the red square is not moving to stay positioned at the center of the w ...

Error message encountered: "Element not located- Capybara unable to find element despite its existence

I’m currently in the process of writing a selenium test using capybara, and I’ve encountered an issue when trying to populate a pop-up box. Specifically, I am unable to input the element’s name using the fill_in method in capybara. Strangely, this me ...

Troubleshooting problems with Bootstrap 3 inline datepicker beforeShowDay

Having created an inline datepicker, I have encountered a single issue - the inability to implement the beforeShowDay option. I utilize PHP to retrieve an array from a database, incorporating it with AJAX. Here's the code snippet: $(document).ready ...