Get all input fields inside div (without JS library)

前端 未结 6 519
予麋鹿
予麋鹿 2020-12-29 05:49

What\'s the easiest way to get all input fields inside a div without using a javascript library like jQuery? Similar to this jQuery snippet:

var inputs = $(\         


        
相关标签:
6条回答
  • 2020-12-29 06:16

    getElementsByTagName

    0 讨论(0)
  • 2020-12-29 06:17
    document.getElementById('mydiv').getElementsByTagName('input')
    
    0 讨论(0)
  • 2020-12-29 06:21

    querySelector and querySelectorAll will fetch the details of what you're expecting easily.

    var divElem = document.getElementById("myDiv");
    var inputElements = divElem.querySelectorAll("input, select, checkbox, textarea");
    

    It will give all the input, select, textarea elements in array format.

    0 讨论(0)
  • 2020-12-29 06:21
    document.getElementById("mydiv").getElementsByTagName("input");
    
    0 讨论(0)
  • 2020-12-29 06:34

    If you are on modern browsers (ie9+) you can take advantage of querySelectorAll‎.

    var inputs = document.querySelectorAll‎('#myDiv input');
    
    0 讨论(0)
  • 2020-12-29 06:37

    Try:

    var inputs = document.getElementById('mydiv').getElementsByTagName('input');
    
    0 讨论(0)
提交回复
热议问题