-1

i am calling this function

<script>
function getvalue(){
$(document).ready(function(){
 $("[type='radio']").change(function ()
 {
 var hidden = $(this).prevAll("[type='hidden']");
 var h3 = $(this).prevAll("h3");
 hidden.val(h3.text());
 //alert(hidden.val());
 });
 });
}
</script>

what happens is, clicking on any radio button doesnt fire the function.. but clicking on second does, i have already used document.ready but its not working.. any point which i am missing? Thanx :)

asked Oct 31, 2014 at 13:14
3
  • Clicking on second what? Commented Oct 31, 2014 at 13:16
  • @JayBlanchard clicking on any other radio button second time.. Commented Oct 31, 2014 at 13:18
  • 1
    Can you post your HTML? Commented Oct 31, 2014 at 13:18

2 Answers 2

1

your setting up the change hook for all radio buttons, when you execute the function for the first time. Thus, no change handler attached for the first.

remove the outer function wrapper to install the hook on dom-ready:

<script>
$(document).ready(function(){
 $("[type='radio']").change(function ()
 {
 var hidden = $(this).prevAll("[type='hidden']");
 var h3 = $(this).prevAll("h3");
 hidden.val(h3.text());
 //alert(hidden.val());
 });
 });
</script>
answered Oct 31, 2014 at 13:20
Sign up to request clarification or add additional context in comments.

Comments

1

Remove function getvalue(){..... or you call the function getvalue. This code not register the event handler "ready" and so not fire

 <script>
 $(document).ready(function(){
 $("[type='radio']").change(function ()
 {
 var hidden = $(this).prevAll("[type='hidden']");
 var h3 = $(this).prevAll("h3");
 hidden.val(h3.text());
 //alert(hidden.val());
 });
 });
 </script>

or

 <script>
 function getvalue(){
 $(document).ready(function(){
 $("[type='radio']").change(function ()
 {
 var hidden = $(this).prevAll("[type='hidden']");
 var h3 = $(this).prevAll("h3");
 hidden.val(h3.text());
 //alert(hidden.val());
 });
 });
 }
 getvalue();
 </script>
answered Oct 31, 2014 at 13:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.