I am trying to create radio button and labels in one div called Contentarea
. the problem I am having right now is both radio button and label are clickable, anyone would please tell me how can I disable the click event of label in the following code.
<div id="ContentArea" class="contentarea"> </div>
$("#ContentArea").click(function(evt) {
$("#ContentArea").empty();
$("#lblarea").empty();
$("#btngroup").empty();
var clicked = evt.target;
var currentID = clicked.id || "No ID!";
var type = clicked.value;
if (type == "button") {
loadlabelpagebyID(currentID);
} else if (type == "raidobutton") {
loadlabelpagebyID(currentID);
};
});
function loadradiobuttonpagebyID(pageid) {
$.get("HeadachePROData.xml", function(xml) {
$(xml).find('Pages').each(function() {
$(xml).find('Page[id="' + pageid + '"]').each(function(i) {
pagetype = $(this).attr("type");
nextpageid = $(this).attr("nextpage");
backpageid = $(this).attr("backpage");
sessionStorage.setItem("nextpageid", nextpageid);
sessionStorage.setItem("backpageid", backpageid);
$(this).find('nav').each(function(i) {
var btntext = $(this).text();
navpageid = $(this).attr("navpage");
navtype = $(this).attr("type");
var testbtn = $('<input type="radio">', {
value: navtype,
id: navpageid
});
var $radiolabel = $("<label>").text(btntext);
$radiolabel.removeAttr('onclick').off('click');
$("#ContentArea").append(testbtn);
$("#ContentArea").append($radiolabel);
$("#ContentArea").append("<br/><br/>");
});
});
});
});
}
If you disable the input then the label click action will also be disabled. If you want to stop the label click only (which is a bit silly IMO) then you can change the HTML generated so that the input is outside of the label . Don't do that. Labels are clickable for a reason, they make the control easier to use.
A label can't be disabled. One of the effects it has is to extend the click target of a form control, so you probably want to disable the form control instead. However, for some reason, all your labels are associated with the same control (the one with id="u" ), which suggests that you aren't using <label> correctly.
This can also be achieved with just css though please take note, browser support is limited in IE:
label {
pointer-events:none;
}
Browser support https://caniuse.com/#feat=pointer-events
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With