Recently in one of the projects I was working on, I had to swap attributes between two HTML elements. So I was looking out to see if I could do it in a single statement rather than adding/removing one after the other. Guess what, jQuery has a way do it.
If you’re new to jQuery and looking to include jQuery in your project, you might want to checkout getting started with jQuery.
Let us set up the stage first. We shall include
<input> elements. The class on the
input elements should help you to identify what we’re about to do on that particular element.
Assuming you have jQuery loaded, let us use jQuery to add/remove multiple attributes.
First, let us remove the attributes
name and then on the second
input elemement, we shall add the
id and the
In order, to remove multiple attributes, use the
.removeAttr() method and separate the attributes with a space. And, to add multiple attributes, we must use Plain Object of attribute value pairs in the
You can find the full working demo from JSFiddle.net. I hope you found this tutorial helpful.