Reputation: 987
I would like to get action if with attr of an image is between two values : On my code this .w50
is just on the width attr value = 470px
$("img").each(function() {
var image = $(this);
if (image.attr("width") == 470) {
image.parent().addClass('w50');
}
});
how can I write "add this class if attr value is more than 470 and less than 490" ?
Upvotes: 0
Views: 2618
Reputation: 15376
Try the following:
if ((image.attr("width") >= 470) && (image.attr("width") <= 490))
Note that I have included both 470 and 490 because I think this is what you meant.. if you don't need the edges drop both =
.
More details:
The &&
(AND) operator means that both conditions have to be true.
>=
means greater than or equal.
<=
means less than or equal.
You can read more about comparison operators and logical operators.
Upvotes: 3