Handle Control's Attribute Using jQuery

Introduction

This article shows how to handle or target a control's attribute using jQuery. We already talked about how to modify the appearance of controls in a web page when user hovers the mouse or clicks the mouse. For this we used .addClass(), .removeClass(), .css(), .toggleClass() etc. I'm going to cover some cool stuff using a problem and solution approach. So, let's start looking at first problem.

Problem 1

If you simply want to open all URLs on a new tab, a quick jQuery code can do this using .attr() method.

Note: For more read this.

.attr() Method

.attr() gets the value of an attribute for the first element in the set of matched elements.

Problem 2

If you have a website that uses a huge list of images and you forgot to place alt (alternative) text (for when an image is not found, that will be displayed), how to fix this. I mean how you will add a brand new attribute to an existing list of controls? Don't worry, here is the fix.

image001.jpg

Look at the result in the browser.

image002.jpg


In the same way you can handle attributes of any control.