javascript domElement .attr(attributeName,[attributeValue])
javascript domElement .attr(attributeName,[attributeValue])
Target:
Get/Set DOM element attribute.
- getElementById
- getElementsByTagName
- getElementsByClassName
- getElementsByName
- querySelector
- querySelectorAll
In case of DOM NodeList first Node Element is processed for get and set to all elements.
Description :
get/set attribute of DOM element. if attributeValue is passed then it set the attribute and return element itself, otherwise return attribute of element. If attributeValue is blank string then it remove attribute.
Note: after setting the attribute of DOM elements it return the element/elementList.
Syntax:
DOMElement.attr('attributeName',[attributeValue])
DOMNodeList.attr('attributeName',[attributeValue])
HTMLCollection.attr('attributeName',[attributeValue])
Usase:
- document.getElementById('txtName').attr("maxlength",10)
it sets the maxlength of textbox with 10 character and return textbox element - document.getElementById('txtName').attr("maxlength")
it return the maxlength of textbox i.e. "10" - document.getElementById('txtName').attr("maxlength","")
it remove the maxlength attribute of textbox and return textbox element. - document.querySelectorAll("input[type='text']").attr("maxlength",10)
it sets the maxlength of all textboxes with 10 character and return textbox DOMNodeList - document.querySelectorAll("input[type='text']").attr("maxlength")
it return the maxlength of first textbox i.e. "10" - document.querySelectorAll("input[type='text']").attr("maxlength","")
it remove the maxlength attribut of all textboxes and return textbox elements list.
Download MYJS.js
MyJs is a collection of java script extension methods that enhances the JavaScript objects, class object, DOM element, domElementsCollections, Date, String, Number and other data type.
Include in you project landing page (master page) and it will extends your DOM and JavaScript Objects.
Comments
Post a Comment