355

I was under the assumption that if I disabled a div, all content got disabled too.

However, the content is grayed but I can still interact with it.

Is there a way to do that? (disable a div and get all content disabled also)

IAdapter
  • 62,595
  • 73
  • 179
  • 242
juan
  • 80,295
  • 52
  • 162
  • 195

29 Answers29

691

Many of the above answers only work on form elements. A simple way to disable any DIV including its contents is to just disable mouse interaction. For example:

$("#mydiv").addClass("disabledbutton");

CSS

.disabledbutton {
    pointer-events: none;
    opacity: 0.4;
}

Supplement:

Many commented like these: "This will only disallow mouse events, but the control is still enabled" and "you can still navigate by keyboard". You Could add this code to your script and inputs can't be reached in other ways like keyboard tab. You could change this code to fit your needs.

$([Parent Container]).find('input').each(function () {
     $(this).attr('disabled', 'disabled');
 });
QMaster
  • 3,743
  • 3
  • 43
  • 56
Kokodoko
  • 26,167
  • 33
  • 120
  • 197
  • Thanks!! I too have been struggling with disabling a div in FF/Chrome using .prop(disabled, true). This works great in IE, FF, and Chrome! – pelazem Nov 09 '14 at 23:37
  • 29
    +1 for correct answer - You just saved me hours of work!!! *tears in eyes* and *might be in love* - It's also supported by all browsers: http://caniuse.com/#feat=pointer-events – tim-montague Jun 13 '15 at 16:09
  • Although, it doesn't disable scrolling on child elements like div or textarea. – tim-montague Jun 13 '15 at 16:52
  • 11
    I know it's quite late, but still, it is not supported by IE 8, IE 9 and IE 10. Just to let everybody know. http://caniuse.com/#feat=pointer-events – Razort4x Aug 03 '15 at 11:54
  • 1
    very effective HTML5 solution as divs so not have anything like an enabled attribute. – pat capozzi Dec 07 '15 at 21:11
  • 15
    This will only disallow mouse events, but the control is still enabled. – Mario Levrero May 18 '16 at 09:54
  • 51
    Note: With this solution, you can’t interact with this element, or any children of this element, with a mouse, or on a touch device. But you can still tab to it with your keyboard. – Adam Jul 03 '16 at 09:36
  • But the content still able to scroll. – Shesha Aug 30 '16 at 07:08
  • Nice to see HTML5 is still a decade behind Flash/AS3 in 2016. Still no uniform way to have general or fine-grained control over interactivity like AS3 InteractiveObject's mouseEnabled and mouseChildren properties. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/InteractiveObject.html#mouseEnabled Does the CSS 'pointer-events' also cover other user interaction events like keyboard events on the focused control? Ah, yes, 'focus' management. Another area where AS3 absolutely owns JavaScript. – Triynko Sep 15 '16 at 14:09
  • @Kokodoko : I don't see this solution working for me.Is there something specific to browser?Also i would not like to hide but make the screen fade out and unable to click on elements. – NeverGiveUp161 Feb 15 '17 at 16:52
  • What about using tab? Is focus prevented using Tab key? Probably not. This is not a solution. – walv Feb 20 '17 at 02:15
  • `pointer-events: none !important` should also help. And it works in IE11. – Muflix Feb 27 '17 at 12:31
  • 14
    Still accessible through keyboard though. – Tomer W May 06 '17 at 20:43
  • 2
    totally worked. and I have to add that it might be a good idea if this line would be added to the css code : `user-select: none;` so that if there is any selectable item inside the div would be unselectable. – Sarah Aziziyan Aug 29 '17 at 06:09
  • Can I disable all mouse events, not only click but also drag? – Darshan theerth Jan 03 '18 at 04:55
  • doesn't work when i double click on text fields or event producing divs. – saran3h Feb 26 '18 at 06:37
  • +1 for a simple and more elegant solution. other methods for example like mentioned here https://stackoverflow.com/questions/8423812/enable-disable-a-div-and-its-elements-in-javascript doesn't work. – user3900196 Mar 16 '18 at 15:18
  • To me, not the preferred solution because you can still navigate by keyboard. – Hrvoje Batrnek Feb 05 '19 at 22:48
  • with pointer-events:none elements are not treated as disabled. If any user removes pointer-events from developer console of browser then, it can easily play with your page. adding disabled attribute actually signals back that control is disabled. Different browsers treat disabled control differently. For example disabled controls are not posted back. So it is a big security risk. It should not be used. – Manpreet Singh Dhillon Apr 20 '20 at 21:55
  • In my case I think this will be fine because I'm using this setting once a particular radio button is selected. Once that radio button is selected none of the controls that i'm disabling are functional. This just helps to tell the user that. If they really want to tab to the controls and type in them they can I guess, but they won't do anything. – Rich Jun 15 '20 at 19:35
  • inputs are not disabled. Form submit can still make problems. – Blouarf Sep 01 '20 at 07:49
151

Use a framework like JQuery to do things like:

function toggleStatus() {
    if ($('#toggleElement').is(':checked')) {
        $('#idOfTheDIV :input').attr('disabled', true);
    } else {
        $('#idOfTheDIV :input').removeAttr('disabled');
    }   
}

Disable And Enable Input Elements In A Div Block Using jQuery should help you!

As of jQuery 1.6, you should use .prop instead of .attr for disabling.

BNL
  • 7,085
  • 4
  • 27
  • 32
Martin K.
  • 4,669
  • 7
  • 35
  • 49
  • 2
    "manually" selecting all inputs... I'll try that, but shouldn't it be sufficient to mark the div as disabled? – juan Mar 12 '09 at 18:13
  • When I toggle to un-disable, some pagination buttons that need to remain disabled are also toggled... – juan Mar 12 '09 at 18:18
  • You can filter this buttons and do a ".attr('disabled', true);" every time at them! Just do a $('#idOfPagination').attr('disabled', true); after the if{}else{} construct. – Martin K. Mar 12 '09 at 18:21
  • actually their status is controlled elsewhere, it depends on which page of the list I'm paginating I'm on (they don't always need to be disabled). I'd need someway of doing it without altering the content control's original status – juan Mar 12 '09 at 18:23
  • You can check their status also with jquery and save it. Do: $('#idOfPagination').is(':disabled') ? disablePagination = false : disablePagination = true; once in a global area, directly after the page has been loaded. – Martin K. Mar 12 '09 at 19:23
  • Please note that this does not answer the question, since it doesn't disable all div content. It only affects input elements. Many frameworks and layouters use clickable 'li' elements or 'a' elements. These elements have custom mouse over and click events, so they behave in a similar way to buttons. They wont be affected by this. Detaching all click events and reattaching them in a toggle function is possible, but not done with the code statement in this answer. – Kenyakorn Ketsombut Oct 02 '14 at 02:38
  • This should be the answer. – Hrvoje Batrnek Feb 05 '19 at 22:49
  • Just to clarify what others have mentioned elsewhere in this question. removing the parent div's mouse events doesn't really achieve the desired result in my opinion. Since you are still able to tab through the divs content and select buttons and inputs with space. You can't just rely on turning off mouse events to 'disable' a button. – Air Nov 24 '21 at 14:54
71

Here is a quick comment for people who don't need a div but just a blockelement. In HTML5 <fieldset disabled="disabled"></fieldset> got the disabled attribute. Every form element in a disabled fieldset is disabled.

Ortho
  • 849
  • 6
  • 10
  • 2
    This is a great answer - it allows dynamic items to be spawned in a disabled state so long as they're within the block element rather than testing the disabled state on creation - and elements are truly disabled. – salmonmoose Jan 12 '17 at 08:42
  • 3
    This is the best answer. It's the most semantically correct, telling the browser that all inputs within this fieldset should be disabled. It honors the keyboard and doesn't need mouse handling JS unregistration. One note, though, as of the time of this comment, Edge won't inherit the disabled attribute value from parent fieldsets inside of another fieldset. – Stephen Watkins Nov 17 '18 at 21:46
  • Great one, always the best solutions are the simplest one thanks. – Saghachi Apr 13 '20 at 04:23
  • I'd like to add more upvotes if I could. This is exactly what I needed! – Aleksei Nov 17 '20 at 11:31
  • 1
    best answer here ✅ – Matt Lo Aug 06 '21 at 18:58
  • Perfect! 654321 – Henrik Erlandsson May 17 '22 at 13:45
  • Sadly this does not disable hyperlinks – LOST Aug 14 '23 at 19:34
50

I just wanted to mention this extension method for enabling and disabling elements. I think it's a much cleaner way than adding and removing attributes directly.

Then you simply do:

$("div *").disable();
Community
  • 1
  • 1
cletus
  • 616,129
  • 168
  • 910
  • 942
  • This solution may cause side effects in big pages! (No static reference to a div container / Every underlying element is adressed) – Martin K. Mar 13 '09 at 08:40
  • If you are using asp.net you will get a
    when you disable a Panel control. This works for child elements (ie. they become disabled) in IE but not other browsers. You can disable all child form elements in Chrome/Firefox by combining the jquery disable function with...$("div[disabled='disabled'] :input").disable();
    – Stuart Nov 21 '12 at 12:14
19

You can use this simple CSS statement to disable events

#my-div {
    pointer-events:none;
}
Marcio Mazzucato
  • 8,841
  • 9
  • 64
  • 79
16

Wrap the div within the form and fieldset tags:

<form>
  <fieldset disabled>
    <div>your controls</div>
  </fieldset>
</form>
tim-montague
  • 16,217
  • 5
  • 62
  • 51
Sajithd
  • 529
  • 1
  • 5
  • 11
  • 1
    @kofifus might be an issue with browser? fieldset is a standard HTML tag. – Sajithd Nov 22 '20 at 11:59
  • 1
    This is broken HTML. Should be wrapped in a `
    ` tag. "The `
    ` HTML element is used to within a web form" - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/fieldset
    – tim-montague Aug 01 '22 at 00:09
15

The disabled attribute is not part of the W3C spec for DIV elements, only for form elements.

The jQuery approach suggested by Martin is the only foolproof way you're going to accomplish this.

Chris Van Opstal
  • 36,423
  • 9
  • 73
  • 90
14

similar to cletu's solution, but i got an error using that solution, this is the workaround:

$('div *').prop('disabled',true);
// or
$('#the_div_id *').prop('disabled',true);

works fine on me

Kokizzu
  • 24,974
  • 37
  • 137
  • 233
9

If you wanted to keep the semantics of disabled as follows

<div disabled="disabled"> Your content here </div>

you could add the following CSS

div[disabled=disabled] {
  pointer-events: none;
  opacity: 0.4;
}

the benefit here is that you're not working with classes on the div that you want to work with

real_ate
  • 10,861
  • 3
  • 27
  • 48
8

One way to achieve this is by adding the disabled prop to all children of the div. You can achieve this very easily:

$("#myDiv").find("*").prop('disabled', true);

$("#myDiv") finds the div, .find("*") gets you all child nodes in all levels and .prop('disabled', true) disables each one.

This way all content is disabled and you can't click them, tab to them, scroll them, etc. Also, you don't need to add any css classes.

Blueriver
  • 3,212
  • 3
  • 16
  • 33
8

As many answers already clarified disabled is not a DIV attribute. However xHTML means Extensible HTML. It means you can define your own HTML attributes (all Frontend frameworks does that as well). And CSS supports attribute selectors which is [].

Use standard HTML with your defined attribute:

<div disabled>My disabled div</div>

Use CSS:

div[disabled] {
  opacity: 0.6;
  pointer-events: none;
}

NOTE: you can use CSS attribute selector with ID or Class names as well e.g. .myDiv[disabled] {...} Also can apply value filter e.g.: following HTML disabling standard attribute with value div[disabled=disabled] {...}.

Major
  • 5,948
  • 2
  • 45
  • 60
6

Browsers tested: IE 9, Chrome, Firefox and jquery-1.7.1.min.js

    $(document).ready(function () {
        $('#chkDisableEnableElements').change(function () {
            if ($('#chkDisableEnableElements').is(':checked')) {
                enableElements($('#divDifferentElements').children());
            }
            else {
                disableElements($('#divDifferentElements').children());
            }
        });
    });

    function disableElements(el) {
        for (var i = 0; i < el.length; i++) {
            el[i].disabled = true;

            disableElements(el[i].children);
        }
    }

    function enableElements(el) {
        for (var i = 0; i < el.length; i++) {
            el[i].disabled = false;

            enableElements(el[i].children);
        }
    }
Syed Nasir Abbas
  • 1,722
  • 17
  • 11
5

HTML input controls can be disabled using 'disabled' attribute as you know. Once 'disabled' attribute for an input control is set, event handlers associated with such control are not invoked.

You have to simulate above behavior for HTML elements that don't support 'disabled' attribute like div, if you wish.

If you have a div, and you want to support click or a key event on that div, then you have to do two things: 1) When you want to disable the div, set its disabled attribute as usual (just to comply with the convention) 2) In your div's click and/or key handlers, check if disabled attribute is set on the div. If it is, then just disregard the click or key event (e.g. just return immediately). If disabled attribute is not set, then do your div's click and/or key event logic.

Above steps are browser independent as well.

Sabinia
  • 51
  • 1
  • 1
5

How to disable the contents of a <div/>

The CSS pointer-events property alone doesn't disable child elements from scrolling, and it's not supported by IE10 and under for <div/> elements (only for SVG). http://caniuse.com/#feat=pointer-events

To disable the contents of a <div/> on all browsers.

Jquery:

$("#myDiv")
  .addClass("disable")
  .click(function () {
    return false;
  });

CSS:

.disable {
  opacity: 0.4;
}

/* Disable scrolling on child elements */
.disable div,
.disable textarea {
  overflow: hidden;
}

To disable the contents of a <div/> on all browsers, except IE10 and under.

Jquery:

$("#myDiv").addClass("disable");

CSS:

.disable {
  /* Note: pointer-events not supported by IE10 and under */
  pointer-events: none;
  opacity: 0.4;
}

/* Disable scrolling on child elements */
.disable div,
.disable textarea {
  overflow: hidden;
}
tim-montague
  • 16,217
  • 5
  • 62
  • 51
3

This is for the searchers,

The best I did is,

$('#myDiv *').attr("disabled", true);                   
$('#myDiv *').fadeTo('slow', .6);
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
3

As mentioned in comments, you are still able to access element by navigating between elements by using tab key. so I recommend this :

$("#mydiv")
  .css({"pointer-events" : "none" , "opacity" :  "0.4"})
  .attr("tabindex" , "-1");
Hossein Khalesi
  • 111
  • 1
  • 7
3

Or just use css and a "disabled" class.
Note: don't use the disabled attribute.
No need to mess with jQuery on/off.
This is much easier and works cross browser:

.disabled{
    position: relative;
}
.disabled:after{
    content: "";
    position: absolute;
    width: 100%;
    height: inherit;
    background-color: rgba(0,0,0,0.1);
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

Then you can shut it on and off when initializing your page, or toggling a button

if(myDiv !== "can be edited"){
    $('div').removeClass('disabled');
} else{
    $('div').addClass('disabled');
}
J_sdev
  • 341
  • 1
  • 13
2

I thought I'd chip in a couple of notes.

  1. < div > can be disabled in IE8/9. I assume this is "incorrect", and it threw me off
  2. Don't use .removeProp(), as it has a permanent effect on the element. Use .prop("disabled", false) instead
  3. $("#myDiv").filter("input,textarea,select,button").prop("disabled", true) is more explicit and will catch some form elements you would miss with :input
Steve11235
  • 2,849
  • 1
  • 17
  • 18
1

I would use an improved version of Cletus' function:

 $.fn.disable = function() {
    return this.each(function() {          
      if (typeof this.disabled != "undefined") {
        $(this).data('jquery.disabled', this.disabled);

        this.disabled = true;
      }
    });
};

$.fn.enable = function() {
    return this.each(function() {
      if (typeof this.disabled != "undefined") {
        this.disabled = $(this).data('jquery.disabled');
      }
    });
};

Which stores the original 'disabled' property of the element.

$('#myDiv *').disable();
koosvdkolk
  • 341
  • 2
  • 4
1

Below is a more comprehensive solution to masking divs enabling

  • no separate CSS
  • cover the whole page or just an element
  • specify mask color and opacity
  • specify Z-index so you can show popups over the mask
  • show an hourglass cursor over the mask
  • removing the masking div on maksOff so a different one can be shown later
  • stretch mask when element resize
  • return the mask element so you can style it etc

Also included is hourglassOn and hourglassOff which can be used separately

// elemOrId - jquery element or element id, defaults to $('<body>')'
// settings.color defaults to 'transparent'
// settings.opacity defaults to 1
// settings.zIndex defaults to 2147483647
// if settings.hourglasss==true change cursor to hourglass over mask
function maskOn(elemOrId, settings) {
    var elem=elemFromParam(elemOrId);
    if (!elem) return;

    var maskDiv=elem.data('maskDiv');
    if (!maskDiv) {
        maskDiv=$('<div style="position:fixed;display:inline"></div>');
        $('body').append(maskDiv);
        elem.data('maskDiv', maskDiv);
    }

    if (typeof settings==='undefined' || settings===null) settings={};
    if (typeof settings.color==='undefined' || settings.color===null) settings.color='transparent';
    if (typeof settings.opacity==='undefined' || settings.opacity===null) settings.opacity=1;
    if (typeof settings.zIndex==='undefined' || settings.zIndex===null) settings.zIndex=2147483647;
    if (typeof settings.hourglass==='undefined' || settings.hourglass===null) settings.hourglass=false;

    // stretch maskdiv over elem
    var offsetParent = elem.offsetParent();
    var widthPercents=elem.outerWidth()*100/offsetParent.outerWidth()+'%';
    var heightPercents=elem.outerHeight()*100/offsetParent.outerHeight()+'%';
    maskDiv.width(widthPercents);
    maskDiv.height(heightPercents);
    maskDiv.offset($(elem).offset());

    // set styles
    maskDiv[0].style.backgroundColor = settings.color;
    maskDiv[0].style.opacity = settings.opacity;
    maskDiv[0].style.zIndex = settings.zIndex;

    if (settings.hourglass) hourglassOn(maskDiv);

    return maskDiv;
}

// elemOrId - jquery element or element id, defaults to $('<body>')'
function maskOff(elemOrId) {
    var elem=elemFromParam(elemOrId);
    if (!elem) return;

    var maskDiv=elem.data('maskDiv');
    if (!maskDiv) {
        console.log('maskOff no mask !');
        return;
    }

    elem.removeData('maskDiv');
    maskDiv.remove();
}

// elemOrId - jquery element or element id, defaults to $('<body>')'
// if decendents is true also shows hourglass over decendents of elemOrId, defaults to true
function hourglassOn(elemOrId, decendents) {
    var elem=elemFromParam(elemOrId);
    if (!elem) return;

    if (typeof decendents==='undefined' || decendents===null) decendents=true;

    if ($('style:contains("hourGlass")').length < 1) $('<style>').text('.hourGlass { cursor: wait !important; }').appendTo('head');
    if ($('style:contains("hourGlassWithDecendents")').length < 1) $('<style>').text('.hourGlassWithDecendents, .hourGlassWithDecendents * { cursor: wait !important; }').appendTo('head');
    elem.addClass(decendents ? 'hourGlassWithDecendents' : 'hourGlass');
}

// elemOrId - jquery element or element id, defaults to $('<body>')'
function hourglassOff(elemOrId) {
    var elem=elemFromParam(elemOrId);
    if (!elem) return;

    elem.removeClass('hourGlass');
    elem.removeClass('hourGlassWithDecendents');
}

function elemFromParam(elemOrId) {
    var elem;
    if (typeof elemOrId==='undefined' || elemOrId===null) 
        elem=$('body');
    else if (typeof elemOrId === 'string' || elemOrId instanceof String) 
        elem=$('#'+elemOrId);
    else
        elem=$(elemOrId);

    if (!elem || elem.length===0) {
        console.log('elemFromParam no element !');
        return null;
    }

    return elem;
}

With this you can do for example:

maskOn(); // transparent page mask
maskOn(null, {color:'gray', opacity:0.8}); // gray page mask with opacity
maskOff(); // remove page mask
maskOn(div); // transparent div mask
maskOn(divId, {color:'gray', hourglass:true}); // gray div mask with hourglass
maskOff(div); // remove div mask

see jsfiddle

kofifus
  • 17,260
  • 17
  • 99
  • 173
  • Your solutions is very well to disable whole page but it's not working on particular div portion dear, I have tried. – 3 rules Sep 14 '16 at 11:37
1
function disableItems(divSelector){
    var disableInputs = $(divSelector).find(":input").not("[disabled]");
    disableInputs.attr("data-reenable", true);
    disableInputs.attr("disabled", true);
}

function reEnableItems(divSelector){
    var reenableInputs = $(divSelector).find("[data-reenable]");
    reenableInputs.removeAttr("disabled");
    reenableInputs.removeAttr("data-reenable");
}
Jtbs
  • 181
  • 9
0
$("#yourdivid textarea, #yourdivid input, #yourdivid select").attr('disabled',true);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
anand
  • 512
  • 6
  • 9
0

This css only/noscript solution adds an overlay above a fieldset (or a div or any other element), preventing interaction:

fieldset { position: relative; }
fieldset[disabled]::after { content: ''; display: inline-block; position: absolute; top: 0; left: 0; right: 0; bottom: 0; pointer-events: all; background: rgba(128,128,128,0.2); }

If you want an invisible i.e. transparent overlay, set the background to e.g. rgba(128,128,128,0), as it won't work without a background. The above works for IE9+. The following much simpler css will work on IE11+

[disabled] { pointer-events: none; }

Chrome

Costas
  • 171
  • 2
  • 4
0

If you are simply trying to stop people clicking and are not horrifically worried about security - I have found an absolute placed div with a z-index of 99999 sorts it fine. You can't click or access any of the content because the div is placed over it. Might be a bit simpler and is a CSS only solution until you need to remove it.

Ukuser32
  • 2,147
  • 2
  • 22
  • 32
0

Its very easy to handle if you want to disable the pointer event

document.getElementById("appliedDatepicker").style.pointerEvents = "none";

or

if you want to enable,

document.getElementById("appliedDatepicker").style.pointerEvents = "auto";
0

Another way, in jQuery, would be to get the inner height, inner width and positioning of the containing DIV, and simply overlay another DIV, transparent, over the top the same size. This will work on all elements inside that container, instead of only the inputs.

Remember though, with JS disabled, you'll still be able to use the DIVs inputs/content. The same goes with the above answers too.

TheCarver
  • 19,391
  • 25
  • 99
  • 149
  • What if the user tabs through the controls? This doesn't help at all unless you ONLY have users that use the mouse to navigate. – Sivvy Jan 25 '12 at 14:38
  • But it can be useful in conjunction with disabling the inputs. If the overlaying div is styled as translucent, it is a good visual indicator that the section is disabled. – xr280xr Jul 02 '15 at 23:15
-1

EDIT: Below I've used .on() method, instead use .bind() method

$(this).bind('click', false);
$(this).bind('contextmenu', false);

to remove your setting, you can use .unbind() method. Whereas the .off() method doesn't work as expected.

 $(this).unbind('click', false);
 $(this).unbind('contextmenu', false);

After researching hundreds of solutions! learning about pointer-events, below is what I did.

As @Kokodoko mentioned in his solution which is apt for all browsers except IE. pointer-events work in IE11 and not in the lower versions. I also noticed in IE11, pointer-events do not work on the child elements. And hence if we have something like below

 <a href="www.preshmalinetpereira.wordpress.com"><i class="car icon"></i><span>My Blog</span></a>

where span -is the child element, setting pointer-events: nonewont work

To overcome this problem I wrote a function which could act as pointer-events for IE and will work in the lower versions.

In JS File

DisablePointerEvents(".DisablePointerEvents");


function DisablePointerEvents(classId) {
    $(classId).each(function () {
        $(this).on('click', false );
        $(this).on('contextmenu', false );
    });
}

In CSS File

.DisablePointerEvents{
    pointer-events: none;
    opacity: 0.7;
    cursor: default;
}

In HTML

 <a href="www.preshmalinetpereira.wordpress.com" class="DisablePointerEvents"><i class="car icon"></i><span>My Blog</span></a>

This faked the pointer-events scenario where pointer-events doesnt work and when the above condition of child elements occur.

JS Fiddle for the same

https://jsfiddle.net/rpxxrjxh/

Community
  • 1
  • 1
-1

the simpleset solution

look at my selector

$myForm.find('#fieldsetUserInfo input:disabled').prop("disabled", false);

the fieldsetUserInfo is div contains all inputs I want to disabled or Enable

hope this helps you

Basheer AL-MOMANI
  • 14,473
  • 9
  • 96
  • 92
-1

There are configurable javascript libraries that take in a html string or dom element and strip out undesired tags and attributes. These are known as html sanitizers. For example:

E.g. In DOMPurify

DOMPurify.sanitize('<div>abc<iframe//src=jAva&Tab;script:alert(3)>def</div>'); 
// becomes <div>abcdef</div>
Lee
  • 29,398
  • 28
  • 117
  • 170