select multiple jquery inputs using the name attribute

Asked

Viewed 440 times

0

I have a jquery script that selects a particular Select via the 'name' attribute and makes some changes to the label of another select:

var selectRecheioGroup = $('select[name=recheio1] .grupoRecheio');

The question is: how can I make this selection to also cover other 2 selects of names 'recheio2' and 'recheio3'?

1 answer

2


With jQuery, you can use all those selectors. For your case, the most practical is the ^. Has $("[title^='Tom']") selects all elements that have the attribute started by Tom. See below as it would be for your example, where I apply a padding of 20 for the three elements.

$(function () {
  $("select[name^=recheio].grupoRecheio").css("padding", 20);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select name="recheio1" class="grupoRecheio">
  <option>Recheio 1</option>
</select>

<select name="recheio2" class="grupoRecheio">
  <option>Recheio 2</option>
</select>

<select name="recheio3" class="grupoRecheio">
  <option>Recheio 3</option>
</select>

  • In my case here it worked very well, Anderson, thank you! Now, in case the other 'Names' are not similar, what other alternative would I have?

  • Explain further "Similar Issues".

  • For example, in the solution you gave, all 'Names' have the combination 'stuffing' in the composition of the name: recheio1, recheio2, recheio3. And if they were, for example, 'stuffing', 'decorating' and 'chocolates''?

  • There only on the same arm, selecting each one individually.

  • I get it. I thought there was some solution putting a comma like var selectRecheioGroup = $('select[name=recheio1], [name=recheio2], [name=recheio3] .grupoRecheio');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.