Q:

Find the data passed with the on() method for each <p> element using jquery

0

Find the data passed with the on() method for each <p> element. using jquery

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

HTML Code:

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <title>Click on each header element to return the data passed with the on() method</title>
</head>
<body>
<p>Click on each header element to return the data passed with the on() method.</p>
<h3>This is a header1.</h3>
<h3>This is another header2.</h3>
</body>
</html>

JavaScript Code:

$("h3").each(function(i){
   $(this).on("click", {x:i}, function(event){
     var info = "Header" + $(this).index() + ". paragraph has Event data: " + event.data.x;    
    $("<p>"+info+"</p>").appendTo( "body" );      
   });
});

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now