YouHaveNoSkill
YouHaveNoSkill

Reputation: 53

Disable all inputs with the same array name

Is it possible using jQuery css like selectors?

<input type="text" name="user[name]" />
<input type="text" name="user[age]" />
<input type="text" name="user[height]" />

I was thinking of something like

$('[name="user"]´).prop('disabled', true);

Upvotes: 1

Views: 864

Answers (2)

Sanchit Patiyal
Sanchit Patiyal

Reputation: 4920

Yes you can do it with the ^= operator

$('input[name^="user"]').prop('disabled', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="user[name]" />
<input type="text" name="user[age]" />
<input type="text" name="user[height]" />

P.S. You have a typo in $('[name="user"]´), at the end after ] ´ should be '

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337691

You can use the 'attribute starts with' selector to achieve what you require:

$('[name^="user"]').prop('disabled', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input type="text" name="user[name]" />
<input type="text" name="user[age]" />
<input type="text" name="user[height]" />

Note the ^= operator. More info in the jQuery documentation

Upvotes: 6

Related Questions