Check if there is an attribute on an element using jquery

In this programming tutorial we will learn how to check if there is an attribute on an element or not using jquery. We will use attr() function of jquery, yeah jquery because it is very powerful for performing operations over DOM. So let's have a look over code snippet given below in which we will check whether txtName textbox contains name attribute or not.

var attrExists = $("#txtName").attr("name");

// In some browsers, "attrExists" is undefined; for others,  "attrExists" is false. 
// Check for both.
if (typeof attrExists!== 'undefined' && attrExists !== false) {

    // Attribute Exists
}

So that' it.
I love your feedback.




0 comments: