Faraz Durrani Faraz Durrani - 2 months ago 5
HTML Question

How do I select a id inside bootstrap's label tag

I have html like this:

`<label class="radio-inline"><input type="radio" disabled="true" id="ojtm.inform_infoy" name="ojtm.inform_info" path="ojtm.inform_info" value="Y">Yes</label>`
....
<script>
$(document).ready(function() {
$("#ojtm.inform_infoy").click(function(event) {
console.log("ojtm.inform_infoYY is clicked: ");
//trying to do something
});
});
</script>


I am trying to get its id using jquery to do something. But for some reason nothing is printing in console. How do I select that id inside label tag? Thanks

Answer

Dot is a metacharacter in jquery, Escape it.

$("#ojtm\\.inform_infoy").click(function(event) {

And the best practice is to avoid using . in id/class attributes as it will cause problems like this. Additionally, you should prefer using change event over radio button instead of using click event.