0

I have a form that I am building and would like to have a javascript to select and manipulate all of the fields that are within the named array:

<input type="text" name="location[street]" value required />
<input type="text" name="location[city]" value required />
<input type="text" name="location[zip]" value required />
<input type="text" name="location[state]" value required />

How can I build a selector for javascript to toggle all of the elements disabled state?

Jquery is possible but not preferred. I would prefer a method that would work without libraries - such as document.getElementsByName().

2 Answers 2

2

I believe that querySelectorAll doesn't have support for a selector to get an element by an attribute, like jQuery would be input[name^="location"](docs). So, try this:

var els = document.querySelectorAll('input');

for (var i = 0; i < els.length; i++)
{
    if (els[i].name.indexOf("location") > -1)
    {
        els[i].disabled = true;
    }
}

Fiddle. I will be glad to hear that I'm wrong and there is a way for doing this only using a selector.

Anyway, you can use the fieldset and make your code more semantic by disabling only the fieldset, if you like: Fiddle.

UPDATE

In order to disable all textarea and select elements as well, just include those tags on the selector:

var els = document.querySelectorAll('input, textarea, select');

Fiddle

Sign up to request clarification or add additional context in comments.

5 Comments

This works, however, it is not including any of the TEXTAREA or SELECT form elements (and this is my fault for not mentioning them in the question). If I am going to have to loop through all of the INPUT elements, I might as well just loop through all of the form elements and do the same comparison.
Changed querySelectorAll() to document.getElementById('form_id').elements, so that non-input form types could be selected as well... but otherwise this answer is correct.
Is using document.querySelectorAll('input, textarea, select') more efficient than using the form.elements?
Well, in this case I think form.elements should fit your needs to, but I really don't know which is better in this case. What you can do is set a class for all your elements starting with location in name tag, then you select them with .location-element. In this case querySelectorAll will make more sense.
use a variable for the length so you are not calculating it over and over.
1

Alternative to queryselector would be getElementsByTagName

var i;
var inputs = document.getElementsByTagName("input");

for (i = 0; i < inputs.length; ++i) {
 var name =  inputs[i].getAttribute("name");
    if(name.indexOf("location") > -1)
    {
    inputs[i].disabled = true;
        console.log(name);

    }
}

link to JSFIddle

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.