Skip to main content

Bug Tracker

Side navigation

#7633 closed bug (invalid)

Opened November 25, 2010 09:30AM UTC

Closed November 25, 2010 02:25PM UTC

$.attr('onclick', 'somefunction()') not working on Google Chrome

Reported by: yajo Owned by:
Priority: undecided Milestone: 1.5
Component: unfiled Version: 1.4.4
Keywords: Cc:
Blocked by: Blocking:
Description

I noticed this bug. In Firefox it works.

Using FF 3.6 and GC 7.

Attachments (0)
Change History (1)

Changed November 25, 2010 02:25PM UTC by jitter comment:1

resolution: → invalid
status: newclosed

Thanks for taking the time to contribute to the jQuery project by writing a bug report!

After checking what you reported I conclude that this isn't a jQuery bug. What you are doing is also very uncommon and the correct jQuery way to do this would be to use $(selector).bind("click", somefunction) (as this is guaranteed to work cross-browser).


But it probably also isn't a bug in Chrome.

I made this test case during checking on your problem. As you can see all buttons but "Click me 4" work in Chrome.

To understand this you need to know that attr(name, value) doesn't set the HTML attribute but uses the DOM property and it looks like Chrome (rightfully) doesn't allow setting the onclick DOM property to a string but only to a function. Other browsers are less strict about this and automagically convert a string assignment into a function. Chrome doesn't.

attr(name, value) using the DOM property also explains why "Click me 7" works but "Click me 4" doesn't, as for "Click me 7" I set the HTML attribute directly and in this case (as when done inline) Chrome converts the value to a function call.

So basically you have just hit a "rough" (or better differing) point in Chrome's DOM implementation. If you understood all of the above and still feel this is a bug then feel free to report this on the Chrome bug tracker.