1

I have several html input elements which all look something like this:

<input id="1" type="radio" checked="checked" value="123" name="myinput" />

How do I fetch the input element with javascript by multiple attributes? For instance I want to fetch the input element which is of type radio, is checked and have a value that equals 123?

Basically I want to do something like this (I know this doesn't work):

var myElement = document.body.select('input[type=radio]:checked', 'input[value=123]');
3
  • your id should be unique so using document.querySelector('#1') should be enough Commented Dec 5, 2011 at 8:50
  • Ok bad example from my part then. My questions remains though... Commented Dec 5, 2011 at 8:53
  • Just replace document.body.select with document.querySelectorAll. Also, IDs mustn't begin with a number. Commented Dec 27, 2011 at 14:20

2 Answers 2

3

With prototype this should work:

$$("input[type=radio][id=1]:checked")
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a simple function that dose that.

function getInput() {
    var elements = document.getElementsByTagName("input");
    for(var i=1, element=elements[0];element = elements[i++];) {
        if(element.type=="radio" && 
            element.checked==true && 
            element.id=="1") {
            return element;
        }
    }
    return null;
}

Change the if statement inside the loop to your liking.

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.