Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. There are various methods to get an input textbox value directly (without wrapping the input element inside a form element):
  2.  
  3. Method 1:
  4.  
  5. document.getElementById('textbox_id').value to get the value of desired box
  6.  
  7. For example, document.getElementById("searchTxt").value;
  8.  
  9.  
  10.  
  11. Note: Method 2,3,4 and 6 returns a collection of elements, so use [whole_number] to get the desired occurrence. For the first element, use [0], for the second one use 1, and so on...
  12.  
  13. Method 2:
  14.  
  15. Use document.getElementsByClassName('class_name')[whole_number].value which returns a Live HTMLCollection
  16.  
  17. For example, document.getElementsByClassName("searchField")[0].value; if this is the first textbox in your page.
  18.  
  19. Method 3:
  20.  
  21. Use document.getElementsByTagName('tag_name')[whole_number].value which also returns a live HTMLCollection
  22.  
  23. For example, document.getElementsByTagName("input")[0].value;, if this is the first textbox in your page.
  24.  
  25. Method 4:
  26.  
  27. document.getElementsByName('name')[whole_number].value which also >returns a live NodeList
  28.  
  29. For example, document.getElementsByName("searchTxt")[0].value; if this is the first textbox with name 'searchtext' in your page.
  30.  
  31. Method 5:
  32.  
  33. Use the powerful document.querySelector('selector').value which uses a CSS selector to select the element
  34.  
  35. For example, document.querySelector('#searchTxt').value; selected by id
  36. document.querySelector('.searchField').value; selected by class
  37. document.querySelector('input').value; selected by tagname
  38. document.querySelector('[name="searchTxt"]').value; selected by name
  39.  
  40. Method 6:
  41.  
  42. document.querySelectorAll('selector')[whole_number].value which also uses a CSS selector to select elements, but it returns all elements with that selector as a static Nodelist.
  43.  
  44. For example, document.querySelectorAll('#searchTxt')[0].value; selected by id
  45. document.querySelectorAll('.searchField')[0].value; selected by class
  46. document.querySelectorAll('input')[0].value; selected by tagname
  47. document.querySelectorAll('[name="searchTxt"]')[0].value; selected by name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement