Jason G Jason G - 1 year ago 71
HTML Question

Radio Button change / click not working

Dynamically loaded via AJAX

here is the head section

<link rel="stylesheet" type="text/css" href="templates/main.css">
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<script src="js/main.js"></script>

$('.vp_names').on("change", "input[name='vp_names']", function(event) {
alert('got here'); // NEVER FIRES

<div class='vp_names'>
<input id="vp_names_1" checked name='vp_names' value='1' type='radio'/>
<label class="drinkcard-cc label_card male" for="vp_names_1">George</label>
<input id="vp_names_2" name='vp_names' value='2' type='radio'/>
<label class="drinkcard-cc label_card male" for="vp_names_2">Jungle</label>

Variations of the above code work for all my other inputs / buttons on the same page / load. Range inputs work, other radio / checkboxes work. I've tried change / click. I've tried with :radio and without. I've tried changing the names, changing the selector, changing the parent selector, nothing will get the radio buttons to fire.

** Update **

while not figuring out why it was not working, as it works this way in other sections of the code, changing it to the following did work. So if you are having a similar issue, try this.

$('#view_profile').on("change", ".vp_names", function(event) {
alert('got here');


<input class='vp_names' id="vp_names_1" checked name='vp_names' value='1' type='radio'/>
<label class="drinkcard-cc label_card male" for="vp_names_1">John</label>

Removed class from parent div

I still use the 'bag' of the view_profile, and this is the method for attaching to dynamically loaded content via ajax.

** Solution **

Appears to have been a JS cache issue as suggested by Rob.

rob rob
Answer Source

I think your selector is the problem. I'm not even sure if input[name=...] is valid (not that my ignorance is proof that it isn't valid).

Give all the inputs you want to be affected the same class, and then just trigger from the class.

<input type="radio" class="vp_radio" ... /> 

    $('.vp_radio').on('change', function(){ 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download