Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable label click event

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/>");
                });
            });
        });
    });
}
like image 456
allenhe Avatar asked Feb 17 '16 14:02

allenhe


People also ask

How do I disable click event on label?

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.

How do I disable labels?

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.


1 Answers

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

like image 167
jfxninja Avatar answered Sep 20 '22 05:09

jfxninja