troubleshooting Question

Problem: Using jquery's ".live" within an ".each" loop

Avatar of the_sleeper
the_sleeperFlag for United States of America asked on
JavaScriptJScript
4 Comments1 Solution590 ViewsLast Modified:
Greetings,

I've set up a little test to learn how to use jquery's .live options.

3 hard-coded radio buttons
1 dynamic radio button
-----------------------------
Test the click event

After several attempts at using live...all failed. Using .bind() works, but not on the dynamic field ((see code below)) .  How should this script be adapted to use the jquery .live() method?


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {

	$('input[name="rdo_test"]').each(
	
		function(index){
			$(this).bind(
				"click",function()
				{				
					alert("index: " + index + " value = " + $(this).val())	
				}
			) // end bind
		} // end function
	) // end each
	
	
	// dynamic field test
	$('<input type="radio" name="rdo_test" value="4" />Test 4<br />').appendTo('body');
	
});
</script>

</head>

<body>
<input type="radio" name="rdo_test" value="1" />Test 1<br />
<input type="radio" name="rdo_test" value="2" />Test 2<br />
<input type="radio" name="rdo_test" value="3" />Test 3<br />

</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros