bind by class getAttribute example

Last update: 22 Квітня, 2023

Category: JavaScript Code Examples

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>bind demo</title>
        <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
    </head>
    <body>
        <span class="test" indexNumber="112">213123123</span>
        <span class="test" indexNumber="532">325235235</span>
        <p>Click or double click here.</p>
        <span class="res"></span>
        <script>
            $( '.test' ).bind( "click", function( event ) {
                var attribute = this.getAttribute("indexNumber");
                $( '.res' ).text( "Click happened! " + attribute ); 
            });
        </script>
    </body>
</html>