javascript - Uncaught TypeError: scrollIntoView is not a function - Stack Overflow

admin2025-04-22  0

I can't figure out why, but scrollIntoView() function doesn't work. The browser displays an error: Uncaught TypeError: element.scrollIntoView is not a function at .... The element element is defined and displayed in console. I haven't been able to find any answer in the Internet. Below is simplified code:

[...]
<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Surname</th>
        </tr>
    </thead>
    <tbody>
        <?php 
        if (is_array($array)) {
            foreach ($array as $element) { ?>
                <tr data-someid="<?= $element->some_id ?>">
                    <th><?= $element->id ?></th>
                    <td><?= $element->name ?></td>
                    <td><?= $element->surname ?></td>
                </tr>
            <?php } 
        } ?>
    </tbody>
</table>
[...]
<script>
    var element= document.querySelectorAll('[data-someid="' + someId + '"]');
    console.log(element); // displays the element well
    element.scrollIntoView();
</script>
[...]

Do somebody have any ideas why it doesn't work?

I can't figure out why, but scrollIntoView() function doesn't work. The browser displays an error: Uncaught TypeError: element.scrollIntoView is not a function at .... The element element is defined and displayed in console. I haven't been able to find any answer in the Internet. Below is simplified code:

[...]
<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Surname</th>
        </tr>
    </thead>
    <tbody>
        <?php 
        if (is_array($array)) {
            foreach ($array as $element) { ?>
                <tr data-someid="<?= $element->some_id ?>">
                    <th><?= $element->id ?></th>
                    <td><?= $element->name ?></td>
                    <td><?= $element->surname ?></td>
                </tr>
            <?php } 
        } ?>
    </tbody>
</table>
[...]
<script>
    var element= document.querySelectorAll('[data-someid="' + someId + '"]');
    console.log(element); // displays the element well
    element.scrollIntoView();
</script>
[...]

Do somebody have any ideas why it doesn't work?

Share Improve this question edited Feb 2, 2019 at 14:24 Andrew Shaban asked Feb 2, 2019 at 14:13 Andrew ShabanAndrew Shaban 1372 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

document.querySelectorAll() returns a list of DOM elements. Right now you're trying to access a method .scrollIntoView on the returned NodeList which of course doesn't exist.

You probably meant to use document.querySelector() which returns a single element.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745284393a294301.html

最新回复(0)