Bug Tracker

Opened 12 years ago

Closed 11 years ago

Last modified 8 years ago

#2998 closed bug (invalid)

attr('onclick',value)

Reported by: shadow Owned by:
Priority: major Milestone: 1.3
Component: event Version: 1.2.6
Keywords: Cc:
Blocked by: Blocking:

Description

The problem is: we have something like this in html code:

<div class="pages" onclick="loadPage(1)"></div>
<div class="pages" onclick="loadPage(2)"></div>
.
.
.
<div class="pages" onclick="loadPage(n)"></div>

<div id="page"></div>

jQuery code:

function loadPage(page) {
	$.post("url", {
		page:	page
		},
		function (data) {
			v = parseInt($('div[class="pages"]:first').text());
			// putting data returned from .post to div with id 'page'
			$('#page').html(data);

			t = parseInt($('div[class="pages"]:first').text());

			// Rewriting onclick
			$('div[class="pages"]').attr('onclick', "loadPage('" + v + "')");
			$('div[class="pages"]:contains(' + page + ')').attr('onclick', '');
		}
	);
}

In jQuery 1.2.4 this code works, in versions 1.2.5,1.2.6 don't.
After 1 execution of loadPage() (rewriting onclick with attr() within function loadPage) browers don't catch click event anymore.

Change History (1)

comment:1 Changed 11 years ago by flesler

Component: coreevent
Resolution: invalid
Status: newclosed

This doesn't work on IE. Inline/obstrusive string event handlers are not really supported. We decided that there's no point in handling something like this, because isn't crossbrowser and it's deprecated coding.

You should use bind or at least, functions as handlers. Don't use function with .attr(). Use .each and set them on each element.

$().each(function(){
   this.onclick = function(){
     // ....
   };
});
Note: See TracTickets for help on using tickets.