Nedaye Imroz
Nedaye Imroz

Reputation: 1

Dynamic Radio Button Group .On Handler (jquery)

I have two radio buttons (level 1) which dynamically creates a list of new radio buttons (level 2) based on the value selected.

The code that retrieves the list within is listed here

while($row=odbc_fetch_array($res))
{
$dept = $row['dept'];
echo "<input type='radio' class='radio' value='".$dept."' name='lvl2'/>".$dept." &nbsp  &nbsp &nbsp &nbsp ";
}

I am using

$('input[name="lvl1"]').click(function(){} 

to get values from the first set of radio buttons. However

$('input[name="lvl2"]').on('click', function(){
        $("span").text ("hi");
});

is not working.

I will be drilling further into level 3 and level 4 of dynamic buttons and need to get the on click handler working to pass values into SQL script for each level.

Upvotes: 0

Views: 778

Answers (1)

adeneo
adeneo

Reputation: 318182

If the radio's are dynamic, you'll have to delegate the event:

$(document).on('click', 'input[name="lvl2"]', function(){
    $("span").text ("hi");
});

and replace document with closest non dynamic parent, and using the change event will work even if someone decides to use a keyboard.

Upvotes: 2

Related Questions