removeAttr inside string html

I want to remove attr from string html. I have this code

 var htm = result.Html;
            $(htm, 'div').each(function () {
                    $(this).removeAttr('ondragover');
                    $(this).removeAttr('ondragleave');
                    $(this).removeAttr('ondrop');
            });
            $('#divLayout').html(htm);

but the problem the string stay as orginal notice: result.Html equal:

    <div class="updiv containertwo" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>
    <div id="fchoise" class="detdiv containertwo">
        <div id="df4a6783-beb2-2cdf-0b1d-611c4d7b195f" class="div1" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>
    </div>
    <div class="updiv containertwo" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>
    <div id="ghchoise" class="containertwo detdiv">
        <div id="932e29b5-b6fe-97f5-d3dc-21768291ec90"  class="lefts" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>
        <div id="cfac8011-0e4e-3eba-aaaa-ac36b58b1512"  class="rights" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>
    </div>
    <div class="downdiv containertwo" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>
    <div id="thchoise" class="containertwo detdiv">
        <div id="3b8b92b3-45f9-54b2-b01a-60b60f65f175"  class="lefts" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>
        <div id="c73e2dc9-9980-774b-5d50-c35336d8201d"  class="rights" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>
    </div>
    <div class="downdiv containertwo" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>

Answer

changing a jQuery object will not modify the original string, so you need

var htm = result.Html;

var $tmp = $('<div />', {
    html: htm
});
$tmp.find('div[ondragover]').removeAttr('ondragover');
$tmp.find('div[ondragleave]').removeAttr('ondragleave');
$tmp.find('div[ondrop]').removeAttr('ondrop');

$('#divLayout').html($tmp.html());

Demo: Fiddle

Leave a Reply

Your email address will not be published. Required fields are marked *