Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onclick="func()" or .click()?

Tags:

jquery

I was wondering which one of the above methods is preferred or is the general guideline that you use $(elem).click() for simple things and < a onclick="func(1, 2, 3, 4);" > when you need to pass parameters to the script when the element is clicked? I've been trying to only use $(elem).click() to keep the code clean and simple, but when you have to pass more than one parameter it gets tricky and function call on onclick="" seems like a better solution.

like image 627
TheMagician Avatar asked Oct 25 '09 03:10

TheMagician


People also ask

Is Onclick and click Same?

click is a function on HTML elements you can call to trigger their click handlers: element. click(); onclick is a property that reflects the onclick attribute and allows you to attach a "DOM0" handler to the element for when clicks occur: element.

What is Click () method?

click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event.

Is Onclick a function or method?

The JavaScript onclick function is designed to execute code when users interact with the HTML elements. The onclick JavaScript can be applied to any HTML element.

What is difference between click and Onclick in jQuery?

So, using a string that is tied to a function, onclick produces an attribute within the binded HTML tag. . click, on the other hand, attaches the function to the property element.


1 Answers

One of the main ideas behind jQuery is unobtrusive javascript (i.e. HTML and javascript are two great tastes that don't taste great together). Using the jQuery object and selectors to attach the functions is what they would prefer you do.

like image 151
Jason Punyon Avatar answered Nov 04 '22 01:11

Jason Punyon