Bug Tracker

Opened 8 years ago

Closed 8 years ago

#9831 closed bug (invalid)

children() bug

Reported by: anonymous Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.6.2
Keywords: Cc:
Blocked by: Blocking:

Description

to reproduce

run the following code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>document</title>
<style type="text/css">
	select{
		width: 140px;
	}
</style>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script>

(function($){
	$.fn.multiSelect = function(){
		var $this = $(this);
		var selects = $this.children();
		selects.live('click change',function(){
			alert('de');
		});	
	};
})(jQuery);

(function($){
	$.fn.multiSelect2 = function(){
		var $this = $(this);
		var selects = $this.find('select');
		selects.live('click change',function(){
			alert('de');
		});	
	};
})(jQuery);

$(function(){
	$('#catform').multiSelect();
});	
</script>

</head>

<body>
<form id="catform" class="c">
	<select size="10" class="cat1" name="cat1">
		<option value="property" >bất động sản</option>
		<option value="service" >dịch vụ</option>
	</select>
	<select size="10" class="cat2" name="cat2" style="margin:0 15px">
		<option value="job-21" >an ninh - bảo vệ</option>
		<option value="pet-4" >khác</option>
	</select>
	<select size="10" class="cat3" name="cat3">
		<option value="clothing-1-11" >ví</option>
		<option value="computer-11-3" >webcam</option>
	</select>
</form>
		
</body>

</html>

Then try to run multiSelect2 on $('.catform'). you will see, the code is basically the same, but multiSelect 2 will work, multiSelect() will not

Change History (1)

comment:1 Changed 8 years ago by ajpiano

Resolution: invalid
Status: newclosed

Thank you for your time and interest in helping the jQuery project, but I am reasonably confident that this is not a bug. As it is stated on the docs for .live,

DOM traversal methods are not supported for finding elements to send to .live(). Rather, the .live() method should always be called directly after a selector

.children() is a traversal method - as is find - so to be honest, I'm surprised it's working in either case, but the former *certainly* is not supported. This seems like an ideal use case for .delegate over .live

Note: See TracTickets for help on using tickets.