I want to place a function on input radio elements. These input radio elements are created dynamically by using Jquery.One dynamically-created(by user's click) input element looks as follows:
<div class="below">
<input type="radio" value="information" name="option0"/>
information
<input type="radio" value="communication" name="option0"/>
communication
<input type="radio" value="goods" name="option0"/>
goods
<input type="radio" value="attention" name="option0"/>
attention
</div>
I wrote the function as follows:
$('input:radio').click(function () {
if (this.checked) { // or $(this).attr('checked')
alert(this.value); // or alert($(this).val());
}
});
I found that this doesn't work because $('input:radio') can only work on static input radio elements, not dynamically-created input radio elements by using Jquery.
Of course I can embed the callback code in input radio elements, so it looks as follows:
<div class="below">
<input type="radio" value="information" name="option0" onclick="myfunction()" />
information
<input type="radio" value="communication" name="option0" onclick="myfunction()" />
communication
<input type="radio" value="goods" name="option0" onclick="myfunction()" />
goods
<input type="radio" value="attention" name="option0" onclick="myfunction()" />
attention
</div>
But I am wondering is there other elegant way to do it instead?