48

I've seen it's jquery equivalent:

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

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

Thanks for all the responses so far but I'm sure if it is working correctly, I need to change the value of the input into something else. I though I could do this by

<enter snippet to select element here>.value = "someOtherValue";

But it appears to be not that easy. Any ideas.

3
  • 5
    Use getElementsByTagName (defined in DOM) to get a list of all input tags, and then filter them in Javascript code (looking at their value attribute). After you have done this a few times, rethink why you want to avoid jQuery. Commented Jan 19, 2012 at 12:59
  • You can try this stackoverflow.com/questions/34001917/… Another way to resolve this case. Commented Mar 21, 2019 at 20:06
  • Luckily in the past 10 years JavaScript has advanced quite a bit! youmightnotneedjquery.com Commented Nov 27, 2022 at 20:44

8 Answers 8

51

with ie6-ie7-ie8

function getInputsByValue(value)
{
    var allInputs = document.getElementsByTagName("input");
    var results = [];
    for(var x=0;x<allInputs.length;x++)
        if(allInputs[x].value == value)
            results.push(allInputs[x]);
    return results;
}

with modern browsers ie9+ (? not sure for ie9 actually) :

document.querySelectorAll("input[value=something]");
Sign up to request clarification or add additional context in comments.

Comments

19

You can use document.querySelectorAll() on modern browsers (https://developer.mozilla.org/En/DOM/Document.querySelectorAll), e.g.

var byValue = document.querySelectorAll('input[value="something"]');

For older browsers you'll have to iterate over the inputs and check the value, e.g.

var inputs = document.getElementsByTagName("input"),
    i,
    len,
    byVal = [],
    value = "something";

for (i = 0, len = inputs.length; i < len; i++) {
    if (inputs[i].value === value) {
        byVal.push(inputs[i]);
    }
}

Comments

4

Something like this works:

function getCheckboxByValue(v) {
        var inputs = document.getElementsByTagName('input');
        for (var i = 0; i < inputs.length; i++) {
                if(inputs[i].type == "checkbox" && inputs[i].value == v) {
                        return inputs[i];
                }
        }
        return false;
}
(function testCheckbox() {
        getCheckboxByValue("1").checked = true;
})();

Using jQuery would be much better, though.

2 Comments

only returns the first one, though.
I've been working on a similar project. This answer solved my problem. Thanks!
3
var elems = [].filter.call( document.getElementsByTagName("input"), function( input ) {
    return input.value === "something";
});

http://jsfiddle.net/ts2Rr/3/

Comments

0

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);
   }
}

1 Comment

true, a quick amendment will fix that
0

This might be overboard in most cases, but an alternative method would be to use document.evaluate with an XPath expression to select the input field by value:

let result = document.evaluate(
  '//input[@value="something"]',
  document,
  null,
  XPathResult.ANY_TYPE,
  null
).iterateNext();

result.value = 'someOtherValue';

Note: This method is not supported by Internet Explorer.

Otherwise, use one of the other mentioned methods for better browser compatibility and speed.

Comments

0

I found that if you need the name or ID this will work:

${RegRowFullName}= Get Element Attribute xpath=//input[@value="Dragon"] name

Comments

-1

If you want all elements by ID you can use

function getElementsById(elementID){
    var elementCollection = new Array();
    var allElements = document.getElementsByTagName("*");
    for(i = 0; i < allElements.length; i++){
        if(allElements[i].id == elementID){
            elementCollection.push(allElements[i]);
        }
    }
    return elementCollection;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.