Table of contents

M4Q Attributes

Use special attributes functions to work with element attributes.

About

You can work with element attributes with special attributes functions: attr(), removeAttr(), toggleAttr(), id(), prop(), meta(), doctype(), html().

attr()

You can manipulate element attributes with it.

Get all attributes


                    $.each($(selector).attr(), function(key, val){
                        console.log("Attribute " + key + " has value " + val);
                    });
                

Get attribute by name


                    var id = $(selector).attr("id");
                    console.log("Element ID: " + id);
                

Set attribute by name


                    $(selector).attr("id", "element_id");
                

Set attributes


                    $(selector).attr({
                        "id": "element_id",
                        "class": "me-class",
                        "style": "display: none"
                    });
                

removeAttr()

Remove attributes.

Remove all attributes


                    $(selector).removeAttr();
                

Remove attribute by name


                    $(selector).removeAttr("id");
                

Remove attribute by names


                    $(selector).removeAttr("id, class, style");
                

toggleAttr()

Remove or set attribute by name.

Remove attribute


                    $(selector).toggleAttr("id");
                

Set attribute value


                    $(selector).toggleAttr("id", "element_id");
                

id()

Get or set element ID for first element in set.


                    $(selector).id("id", "elementID"); // set
                    console.log( "Element ID: " + $(selector).id("id") ); // get
                

prop()

Get or set element property for all elements in set. You can set hidden, checked and disabled properties. Get return property value for first element in set.


                    $(selector).prop("checked"); // get
                    $(selector).prop("checked", true); // set
                

doctype()

Get page DOCTYPE element.


                    $.doctype(); // get page DOCTYPE
                

html()

Get page html element.


                    $.html(); // get page DOCTYPE
                

meta()

Get meta attributes.


                    $.meta(); // get all document meta attributes
                    $.meta("viewport"); // get meta with name viewport
                

charset()

Get or set meta charset.


                    $.charset(); // get meta charset
                    $.charset("UTF-8"); // set and get meta charset