How to get next element in jquery?

Member

by charles , in category: JavaScript , 2 years ago

How to get next element in jquery?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by dmitrypro77 , 2 years ago

@charles You can use next() method in jQuery to get the next element:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <script src="https://code.jquery.com/jquery-2.2.4.js"></script>
</head>
<body>
    <button id="button">Get Next Element</button>
    <p class="text">Test Element</p>
</body>
<script>
    $("#button").on('click', function () {
        let nextElement = $(this).next()
        // Output: text
        console.log(nextElement.attr('class'))
        // Output: Test Element
        console.log(nextElement.text())
    })
</script>
</html>


by laury_ullrich , a year ago

@charles 

In jQuery, you can use various methods to get the next element. Here are some examples:

  1. next(): The next() method returns the next sibling element of the selected element. It only selects the first sibling that matches the selector. Example:
1
$("selector").next();


  1. nextAll(): The nextAll() method returns all the next sibling elements of the selected element. Example:
1
$("selector").nextAll();


  1. nextUntil(): The nextUntil() method returns all the next sibling elements of the selected element until a specific selector is found. Example:
1
$("selector").nextUntil("specific-selector");


  1. siblings(): The siblings() method returns all the sibling elements of the selected element. Example:
1
$("selector").siblings();


These are some of the methods you can use to get the next element in jQuery. Just replace the "selector" with the element you want to select.