How to select an input element by value using javascript?

后端 未结 7 846
被撕碎了的回忆
被撕碎了的回忆 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);
       }
    }
    

提交回复
热议问题