Navigate to a specific section within a table

My table is quite large, with potentially more than 100 rows. To handle this, I've used the Scroll class to give it a fixed height. Within the table, there is a radio button.

The issue arises when the table is rendered and, for example, row No. 50 is selected by default. I want the table to automatically scroll to that position. Each row has its own class defined in the code, so I simply need to scroll to a particular class within the table.

Here is a snippet of the HTML:

<table class="table-scroll">
<tr class="radio1"><td>1</td></tr>
<tr class="radio2"><td>2</td></tr>
<tr class="radio3"><td>3</td></tr>
</table>

For the CSS:

.table-scroll{
    max-height:500px;
    overflow-x:hidden;
    overflow-y:scroll;
    margin-bottom:20px;
    border-top:1px solid #ccc;
    border-bottom:1px solid #ccc;
}

And for the JavaScript:

<script>
How can I scroll to a specific class such as radio3??
</script>

Answer №1

If you want to scroll an element into view, you can use the scrollIntoView(options) method. Check out the documentation to learn how to pass the options object and ensure browser compatibility.

function scrollToElement() {
    var selectedClass = "radio15";
    var table = document.getElementById("test");
    var element = table.getElementsByClassName(selectedClass)[0];
    element.scrollIntoView();
}
.table-scroll {
    max-height: 100px;
    overflow-y: scroll;
    border: 1px solid #000;
    display: block;
}
<table class="table-scroll" id="test">
    <tbody>
        <tr class="radio1"><td>1</td></tr>
        <tr class="radio2"><td>2</td></tr>
        <tr class="radio3"><td>3</td></tr>
        <tr class="radio4"><td>4</td></tr>
        <tr class="radio5"><td>5</td></tr>
        <tr class="radio6"><td>6</td></tr>
        <tr class="radio7"><td>7</td></tr>
        <tr class="radio8"><td>8</td></tr>
        <tr class="radio9"><td>9</td></tr>
        <tr class="radio10"><td>10</td></tr>
        <tr class="radio11"><td>11</td></tr>
        <tr class="radio12"><td>12</td></tr>
        <tr class="radio13"><td>13</td></tr>
        <tr class="radio14"><td>14</td></tr>
        <tr class="radio15"><td>15</td></tr>
        <tr class="radio16"><td>16</td></tr>
        <tr class="radio17"><td>17</td></tr>
        <tr class="radio18"><td>18</td></tr>
        <tr class="radio19"><td>19</td></tr>
        <tr class="radio20"><td>20</td></tr>
    </tbody>
</table>
<button id="scroll" onclick="scrollToElement()">Scroll to radio15</button>

Answer №2

<tr class="radio3" id="radio3"><td>3</td></tr>
<script>
    $(window).load(function(){

       top.location.href = '#radio3';

    });
</script>

I believe this information will be beneficial for you

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

Different "criteria" for CSS wildcard selectors using Selenium

There are several CSS webelements on my page with ids in the format: cell_[number]-button_[number] I am attempting to target ALL of these elements by using Selenium to locate all elements that begin with cell and include button: var elements = Util.driver ...

What is the best way to transition all elements down while sliding a header up or down?

Is it possible to bring down the entire page when hovering over a header element using CSS, ensuring that the header overlaps nothing else on the page? If so, how can this be achieved? See an example of the header in action here: Thank you! ...

Button for Selecting Colors

I love the color picker button on Google Keep, where a variety of colors pop up for selection. I'd like to add something similar to my website. What steps should I take to implement this feature? ...

Unspecified data transfer between child and parent components

I'm working on implementing a file selection feature in my child component, but I'm facing difficulties passing the selected file to the parent component. I'm struggling to find a solution for this issue and would appreciate some guidance. W ...

Issue with Bootstrap: unable to align columns vertically

I am currently learning how to use bootstrap, but I am facing a challenge with vertical alignment of columns. Despite trying various methods, I can't seem to get the content to align anywhere other than the top. Even starting from scratch with code fr ...

Tips for adjusting UI size in CSS based on viewport dimensions and accommodating image content

https://i.sstatic.net/DMU5s.jpg The elements E1, E2, E3, E4 are all part of the user interface (UI) and are intended to have a hover effect. Additionally, there is a background image included in the design. Currently, this is the progress made on the pro ...

Is there a way to trigger js function calls upon the loading of my .js file?

How can I ensure that the following function calls are executed when the .js file is loaded? Layout.Controls.sidebar(Button.Add); Layout.Controls.sidebar(Button.Init); Layout.Controls.printScreen(Button.Add); Layout.Controls.theme(Button.Add); Layout.Cont ...

Combining Ionic 3 with epubJS

Greetings. I've been attempting to access ePub files in my Ionic 3 test app without success. epubjs has been installed via npm. Here is the package.json contents: { "name": "app_name", "author": "Author", "homepage": "http://example.com/", ...

Abrupt surge in Firestore read operations detected in Next.js application following a period of no modifications - refer to the accompanying visual

https://i.sstatic.net/vim7m.png During the initial 10 days of working on the project, I noticed a consistent range of reads between 100 - 300 per day while regularly refreshing documents from firestore. Days 11-15 were spent away from the project visiting ...

What could be the reason for the malfunctioning dropdown menu in my navigation bar upon clicking it?

After spending hours practicing creating a navbar using Bootstrap 3.3.7, I've hit a roadblock - the dropdown is not working when clicked on. It's frustrating because I have double-checked all my scripts and ensured that I have the latest version ...

Using the latest version of Rails (3.1), integrating Paperclip for file uploads, and

I've been trying to configure Ruby on Rails 3.1 with Paperclip and jQuery fileupload. Although I followed a tutorial from the jQuery fileupload page, I am facing issues with getting Paperclip to process the uploaded file. Here is a brief overview of ...

What is the process for sending a file to the server using JavaScript?

It seems like the process is not as simple as I originally thought. Here is the breakdown of what I am trying to achieve: I am capturing the FileList in my state like this... const [formValues, setFormValues] = useState({ image: null }) <input typ ...

Maximizing Efficiency: Top Techniques for Emphasizing Grid Rows with jQuery

Currently, I am facing an issue with the jQuery code I am using to highlight the selected row in my grid. It seems to be taking longer than anticipated to select the row. Does anyone have suggestions on how I can optimize this code for better performance? ...

Develop an enhancement for the Date object in Angular 2 using Typescript

Using the built-in Date type, I can easily call date.getDate(), date.getMonth()...etc. However, I am looking for a way to create a custom function like date.myCustomFunctionToGetMonthInString(date) that would return the month in a string format such as &a ...

Why do I keep receiving the error message "Cannot set property of undefined"?

In the midst of a small project that involves utilizing nuxt, js, and axios, I encountered an issue when attempting to assign the response data to my formFields object. Despite having declared formFields within the data section, I kept receiving an error m ...

Prevent event bubbling on a link generated by an Angular directive that includes transclusion

I am currently developing a directive that adds a link to a DIV element through transclusion. However, I am facing an issue where I want to execute specific functionality when the link is clicked without being redirected to the dummy href (in this case goo ...

Explaining the process of defining a function and addressing the situation of inserting "variable parameters/arguments" in case the first parameter/argument is deemed incorrect

I came across an interesting article called Callback Hell, which discusses the common practice of handling errors in callbacks. The article mentions that in Node.js, it is typical to designate the first argument of a callback function for error handling pu ...

Displaying real-time server responses using jQuery, PHP, and AJAX

Is there a way to display server response in real-time as it is being sent during a long and time-consuming request? Scenario: 1) A JQuery function sends an AJAX request to the server and opens a Dialog widget to wait for the response to update the conte ...

The button remains in a pressed state when viewed on a mobile device

This button is functioning properly in desktop view, but in mobile view the button remains pressed down. :root { --green: #4285f4; --black: rgba(9, 13, 25, 0.752); --box-shadow: .4rem .4rem 1rem #ccc, -.4rem -.4rem 1rem #fff; --box-shadow-inset: ...

issue with AngularJS custom blur functionality not functioning as expected

I have been attempting to set up notifications similar to this example: http://plnkr.co/edit/GbFioFDNb2OC4ZjARQTp?p=preview However, I have been unable to integrate it into my script successfully. When I click outside the notification box, nothing happen ...