How to select an input element by value using javascript?

后端 未结 7 818
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 19:41

I\'ve seen it\'s jquery equivalent:

$(\'input[value=\"something\"]\');

But how do you select it using pure javascript (no jQuery).

相关标签:
7条回答
  • 2020-12-08 20:13

    Something like this should work...

    for(i in document.getElementsByTagName('input')) {
       if(i.value == 'desiredValue') {
          return i;
       }
    }
    

    Edit: This will return an array of all matches

    var matches = [];
    for(i in document.getElementsByTagName('input')) {
       if(i.value == 'desiredValue') {
          matches.push(i);
       }
    }
    
    0 讨论(0)
提交回复
热议问题