Click here to Skip to main content
15,905,322 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using
.click(function() {
for two functions in same page , one is for a javascript function and another is ajax function , the ajax function is working good until i added the javascript function , if i add javascript function then ajax function stops working , if i remove javascript funtion the ajax functions works good , how do i solve this ?

What I have tried:

this is in javascript
var radioButtons = $("input[type='radio'][name='rr']");
       var radioStates = {};
       $.each(radioButtons, function(index, rd) {
           radioStates[rd.value] = $(rd).is(':checked');
       });

       radioButtons.click(function() {

           var val = $(this).val();
           $(this).attr('checked', (radioStates[val] = !radioStates[val]));

           $.each(radioButtons, function(index, rd) {
               if(rd.value !== val) {
                   radioStates[rd.value] = false;
               }
           });
       });

this is in ajax

echo '

<script>
 $(document).ready(function(){  
      $('.$aaa.''.$input.''.$aaa.' ).click(function(){
		   alert( "clicked" );
           var gender = $(this).val(); 
		   var namee = $("#txtt_name").val();
		   var mark = jQuery("#markvalue").val();
           $.ajax({  
                url:"insert.php",  
                method:"POST",  
                data:{"gender":gender, "name":namee, "question":'.$aa.', "empty":mark}, 
                success:function(data){  
                     $('.$result.').html(data);  
                }  
           });  
      });  
 });  
 </script>  '; 
Posted
Updated 6-Apr-21 4:13am

Well - if you used the simple version of onclick event, that is, the declaration in the element, then you'd have no problem (you'd give each function a different name).
JavaScript
<button onclick="doThis()"><button>
<button onclick="doThat()"><button>

Of course, the above is "real" javaScript.

Alternatively, you could have a single function and send in an object argument (by which I mean something like an id value) which you handle in a switch. One function, many outcdomes (and poor but successful coding).


 
Share this answer
 
You are adding the onclick event to the inputs twice. You need to narrow down your selector's better.
 
Share this answer
 
At a guess, your radiobuttons are stored inside the element which matches the $('.$result.') selector. When your AJAX call returns, you replace the entire contents of that element, including the radiobuttons. Any event handlers previously attached to those elements will no longer fire.

You need to use delegated events instead:
.on() | jQuery API Documentation[^]
JavaScript
$(document).on("click", "input[type='radio'][name='rr']", function(){
    var val = $(this).val();
    radioStates[val] = !radioStates[val];
    $(this).prop('checked', radioStates[val]);
    
    $("input[type='radio'][name='rr']").not(this).each(function(){
        radioStates[this.value] = false;
    });
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900