Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove multiple attributes with jQuery's removeAttr

Tags:

jquery

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?

like image 217
somecallmejosh Avatar asked Dec 05 '12 14:12

somecallmejosh


People also ask

How to remove multiple attributes in JavaScript?

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").

How to remove attribute from div in jQuery?

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.

How to add multiple Attr using jQuery?

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, ...})


2 Answers

Yes :

.removeAttr('height align style') 

From the documentation :

as of version 1.7, it can be a space-separated list of attributes.

like image 107
Denys Séguret Avatar answered Oct 13 '22 17:10

Denys Séguret


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 }); 
like image 39
Pritam Jyoti Ray Avatar answered Oct 13 '22 16:10

Pritam Jyoti Ray