Tuesday, March 6, 2012

How to remove multiple attributes using jquery

<div class="Test" id="removeAttributesTest" data-id="1" data-name="ThulasiRam.S"
        style="color: Red;"
>
        <h6>
            Testing to remove multiple attributes using jquery.
        </h6>
 </div>

<script type="text/javascript"> 
(function ($) {     

     $.fn.removeAttrs = function (value) {
        return this.each(function () {
            var obj = $(this);
            if (value && typeof value === 'string') {
                $.each(value.split(' '), function (i, val) {
                    if ($.trim(val).length > 0) {
                        obj.removeAttr($.trim(val));
                    }
                });
            }
        });
    };
})(jquery);

</script>
<script type="text/javascript">
    $(document).ready(function () {  
                                                         //use space for removing multiple attributes.
              $('#removeAttributesTest').removeAttrs('data-id data-name style');
    });
</script>

No comments:

Post a Comment