Difference between click vs onclick event in Jquery

Posted By : Vinay Tiwari | 27-Sep-2017

This blog explain us, how to work .click() and .on('click') event in jquery .on('click') is different from .click(), there it has the capacity to create delegated event handlers by passing a selector parameter, while .click() does not. When .on('click') is called without a selector parameter, it behaves same as .click(). In case you want event delegation, use .on('click').

 

Now, we will define the code structure

 

HTML

 


 

 

Jquery

 

	$(document).ready(function(){
		$('input[type="button"]').click(function(){
			var button = '';
			$('.btn').append(button);
		});
	});
		
	$(".btn").on("click", "#button2", function(){
		alert("10")
	});

 

Declaration

 

In above code, I have to take a button and applying an event .click() on it, in result to it that button will be appended on the div. Secondly, I am applying an.on('click') event and showing an alert in the result.

 

Thanks

 

About Author

Author Image
Vinay Tiwari

Vinay is a bright UI developer, having knowledge of HTML, CSS, Bootstrap, Jquery and AngularJs. His hobbies are interacting with people, listening music etc.

Request for Proposal

Name is required

Comment is required

Sending message..