javascript : DomElement .val(value)

 

javascript : DomElement .val(value)


Target:

Applicable with any DOM input object having attribute value.

  • getElementById
  • getElementsByTagName
  • getElementsByClassName
  • getElementsByName
  • querySelector
  • querySelectorAll

In case of DOM NodeList first Node Element is processed for get/set.


Description :

get/set value attribute of input element. if value is passed then it set the value and return element itself, otherwise return value of element.


Note: after setting the value of input control it return the control itself.


Syntax:

DOMInputElement.val('value')

DOMInputNodeList.val('value')

HTMLCollection.val('value')


Usase:

  1. document.getElementById('txtName').val("Govind Gupta")
    it sets the value of textbox with value "Govind Gupta" and return textbox element
  2. document.querySelector('#txtName').val("Govind Gupta")
    it sets the value of textbox with value "Govind Gupta" and return textbox element
  3. document.getElementById('txtName').val()
    it returns the value of textbox, that is "Govind Gupta"
  4. document.querySelector('#txtName').val()
    it returns the value of textbox, that is "Govind Gupta"
  5. document.getElementById('txtName').val("Govind Gupta").val()
    it set the value of textbox with "Govind Gupta" and then get the value of textbox, that is "Govind Gupta"
  6. document.querySelectorAll('input[type="text"]').val("Govind Gupta")
    it sets the first node's value with value "Govind Gupta" and return NodeList
  7. document.querySelectorAll('input[type="text"]').val()
    it returns the value of first textbox's, that is "Govind Gupta"



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

Popular posts from this blog

javascript : String.right

javascript : String.left

javascript : .nextAll()