pstein Posted November 22, 2016 Share Posted November 22, 2016 Assume I have the following HTML source code: <img onload="..." ... someotherattr="..." ...> ... </img> How can I remove the two attributes "onload" and/or "someotherattr" but not all other (possibly existing) attributes? Can I put the remove command for the two attributes into one statement or do I have to setup two commands (for one attribute per command)? jQuery can be used if it is simpler Thank you Peter Link to comment Share on other sites More sharing options...
dsonesuk Posted November 22, 2016 Share Posted November 22, 2016 Remove these from html element manually would be the easier option, as you need element to exist to target and remove attributes, but! on rendering the elements onload event would kick in anyway, so you would lose that battle to remove onload attribute before it is triggeted. Link to comment Share on other sites More sharing options...
Crowes Posted November 24, 2016 Share Posted November 24, 2016 For attributes that are feasable to remove after the load of the DOM, use .removeAttribute() http://www.w3schools.com/jsref/met_element_removeattribute.asp Link to comment Share on other sites More sharing options...
dsonesuk Posted November 24, 2016 Share Posted November 24, 2016 Yes! But if element has onload attribute in which you want to remove to prevent it being triggered, the onload will be triggered before a removeAttribute() has a chance to be initiated. The element has to be rendered first before a attempt to remove attribute can attempted, but! If element is rendered the onload is triggered. Link to comment Share on other sites More sharing options...
Girish Dubey Posted November 29, 2016 Share Posted November 29, 2016 Yes! you can remove one or more than one attributes in single a statement by using below statement. $("img").removeAttr( "title" ).removeAttr( "someotherattr" ); Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now