jQuery Method How to - Use next() to access next element








Question

We would like to know how to use next() to access next element.

Answer


<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--from w  ww  . j  a  va2  s. c om-->
            $("p").next().text("asdf");   
        });
    </script>
  </head>
  <body>
        <p>bad or .</p><span>good</span>
    </body>
</html>

The code above is rendered as follows: