I have the following code.
$(document).ready(function(){ $('#listing img') .attr('width', 250) .removeAttr('height').removeAttr('align').removeAttr('style') .wrap('<p />'); });
Is there a more efficient way of removing multiple attributes?
You can remove multiple attributes in jQuery by separating them using spaces. This means that you would have to pass all the attributes you want to remove, separated by space, to the removeAttr() method. So now your code should look similar to the following : $("#rightSection").
The removeAttr() method is an inbuilt method in jQuery which is used to remove one or more attributes from the selected elements. Parameters: This function accepts single parameter attribute which is mandatory. It is used to specify one or more attributes to remove.
jQuery | attr() Method The attr() method in jQuery is used to set or return the attributes and values of the selected elements. To set multiple attributes and values: $(selector). attr({attribute:value, attribute:value, ...})
Yes :
.removeAttr('height align style')
From the documentation :
as of version 1.7, it can be a space-separated list of attributes.
Yes, you can remove it in that way:
$('#listing img').removeAttr('height align style');
you can also add those attributes as follows:
$('#listing img').attr({ height: "20", align: left }).css({ color: red, text-align: center });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With