• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 578
  • Last Modified:

jQuery - trigger from radio button change?

Hi,
I am newbie with jQuery, can someone please tell me what is wrong with my code.

 
<!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>
</head>

<script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.6.2.js"></script>
<script type="text/javascript">

$("#pics_needed").change(function() {  
		alert("hello");
                   alert(this);	
});

</script>

<body>

<input name="pics_needed" id="pics_needed" type="radio" value="1" checked>1 Image
<input name="pics_needed" id="pics_needed" type="radio" value="2" >2 Images
<input name="pics_needed" id="pics_needed" type="radio" value="3" >3 Images
<input name="pics_needed" id="pics_needed" type="radio" value="4" >4 Images
<input name="pics_needed" id="pics_needed" type="radio" value="5" >5 Images


</body>
</html>

Open in new window

0
sabecs
Asked:
sabecs
  • 4
2 Solutions
 
chaitu chaituCommented:
you should give uniquie id for each radio button.

<input name="pics_needed" id="pics_needed" type="radio" value="1" checked>1 Image
<input name="pics_needed" id="pics_needed1" type="radio" value="2" >2 Images
<input name="pics_needed" id="pics_needed2" type="radio" value="3" >3 Images
<input name="pics_needed" id="pics_needed3" type="radio" value="4" >4 Images
<input name="pics_needed" id="pics_needed4" type="radio" value="5" >5 Images


$("#pics_needed").click(function() {  
0
 
chaitu chaituCommented:
check this.
<!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>
</head>

<script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.6.2.js"></script>
<script type="text/javascript">

$(document).ready(function () {

$("input[type='radio']").click(function() {  


		alert($('input:radio[name=pics_needed]:checked').val());
});
});

</script>

<body>

<input name="pics_needed" id="pics_needed" type="radio" value="1" >1 Image
<input name="pics_needed" id="pics_needed" type="radio" value="2" >2 Images
<input name="pics_needed" id="pics_needed" type="radio" value="3" >3 Images
<input name="pics_needed" id="pics_needed" type="radio" value="4" >4 Images
<input name="pics_needed" id="pics_needed" type="radio" value="5" >5 Images


</body>
</html> 

Open in new window

0
 
chaitu chaituCommented:
or you can use name as well;

$("input[name='pics_needed']").click(function() {  
0
 
chaitu chaituCommented:
change also will work.

$("input[name='pics_needed']").change(function() {  


		alert($('input:radio[name=pics_needed]:checked').val());
});
});

Open in new window

0
 
sabecsAuthor Commented:
Thanks chaituu, that perfect.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now