-2

How can I delete the style attribute in an img tag?

<div id='someID'>
    <p>
        <img style="width: 585px;" src="somesrc">
    </p>
</div>

Delete this: (just in JS and by tag)

<div id='someID'>
    <p>
        <img style="" src="somesrc">
    </p>
</div>

I am trying something like $("#someID p img").removeAttribute("style"); but it is not working for me.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
programeer
  • 11
  • 3
  • $('img').attr('style','') would do for you instead of using removeAttr() – Naga Sai A May 04 '18 at 16:12
  • Possible duplicate of [Is it possible to remove inline styles with jQuery?](https://stackoverflow.com/questions/2465158/is-it-possible-to-remove-inline-styles-with-jquery) – Narendra Jadhav May 04 '18 at 16:18
  • it doesn't look similar to me. i think they want to remove the entire style parameter. – omikes May 04 '18 at 16:24

3 Answers3

3

.removeAttribute() is a DOM method. For a jQuery instance, you need to use .removeAttr().

So your options are:

$("#someID p img")[0].removeAttribute("style"); // DOM method

... or

$("#someID p img").removeAttr("style"); // jQuery method

Both of the above will completely remove the style attribute from the element, they will not just empty it <img style="">, which could be done by setting the attribute to an empty string:

$('#someId p img').attr('style', '');

... or, using the DOM method:

$('#someId p img')[0].setAttribute('style', '');
tao
  • 82,996
  • 16
  • 114
  • 150
1

if you want to empty style attribute, then:

$("#someID p img").attr({style:""});
Emeeus
  • 5,072
  • 2
  • 25
  • 37
1

If you want to remove a specific style (instead of the whole style attribute), you could also use:

$('selector').css('width', '')

Or in plain JavaScript:

document.querySelector('selector').style.width = '';

(Assuming you want to delete width)

kwyntes
  • 1,045
  • 10
  • 27