2021-02-06 04:42:36 +00:00
---
id: bad87fee1348bd9aed908626
2021-06-05 00:07:40 +09:00
title: Apunta al mismo elemento con múltiples selectores jQuery
2021-02-06 04:42:36 +00:00
challengeType: 6
forumTopicId: 18322
required:
-
link: 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.0/animate.css'
dashedName: target-the-same-element-with-multiple-jquery-selectors
---
# --description--
2021-06-05 00:07:40 +09:00
Ahora conoces tres maneras de seleccionar elementos: por tipo: `$("button")` , por clase: `$(".btn")` , y por id `$("#target1")` .
2021-02-06 04:42:36 +00:00
2021-09-07 07:47:37 -07:00
Aunque es posible agregar múltiples clases en una sola llamada `.addClass()` , vamos a agregarlos al mismo elemento de *tres maneras diferentes* .
2021-02-06 04:42:36 +00:00
2021-09-07 07:47:37 -07:00
Usando `.addClass()` , agrega solo una clase a la vez al mismo elemento, de tres maneras diferentes:
2021-02-06 04:42:36 +00:00
2021-09-07 07:47:37 -07:00
Agrega la clase `animated` a todos los elementos con el tipo `button` .
2021-02-06 04:42:36 +00:00
2021-09-07 07:47:37 -07:00
Agrega la clase `shake` a todos los botones con la clase `.btn` .
2021-02-06 04:42:36 +00:00
2021-09-07 07:47:37 -07:00
Agrega la clase `btn-primary` al botón con id `#target1` .
2021-02-06 04:42:36 +00:00
2021-09-07 07:47:37 -07:00
**Nota:** Solo debes apuntar a un elemento y agregar solo una clase a la vez. En total, tus tres selectores individuales terminarán agregando las tres clases `shake` , `animated` , y `btn-primary` a `#target1` .
2021-02-06 04:42:36 +00:00
# --hints--
2021-06-05 00:07:40 +09:00
Tu código debe usar el selector `$("button")` .
2021-02-06 04:42:36 +00:00
```js
assert(code.match(/\$\s*?\(\s*?(?:'|")\s*?button\s*?(?:'|")/gi));
```
2021-06-05 00:07:40 +09:00
Tu código debe usar el selector `$(".btn")` .
2021-02-06 04:42:36 +00:00
```js
assert(code.match(/\$\s*?\(\s*?(?:'|")\s*?\.btn\s*?(?:'|")/gi));
```
2021-06-05 00:07:40 +09:00
Tu código debe usar el selector `$("#target1")` .
2021-02-06 04:42:36 +00:00
```js
assert(code.match(/\$\s*?\(\s*?(?:'|")\s*?#target1 \s*?(?:'|")/gi));
```
2021-09-07 07:47:37 -07:00
Solo debes agregar una clase con cada uno de tus tres selectores.
2021-02-06 04:42:36 +00:00
```js
assert(
code.match(/addClass/g) & &
code.match(/addClass\s*?\(\s*?('|")\s*?[\w-]+\s*?\1\s*?\)/g).length > 2
);
```
2021-06-05 00:07:40 +09:00
Tu elemento `#target1` debe tener las clases `animated` , `shake` y `btn-primary` .
2021-02-06 04:42:36 +00:00
```js
assert(
$('#target1 ').hasClass('animated') &&
$('#target1 ').hasClass('shake') &&
$('#target1 ').hasClass('btn-primary')
);
```
2021-06-05 00:07:40 +09:00
Solo debes usar jQuery para agregar estas clases al elemento.
2021-02-06 04:42:36 +00:00
```js
assert(!code.match(/class.*animated/g));
```
# --seed--
## --seed-contents--
```html
< script >
$(document).ready(function() {
});
< / script >
<!-- Only change code above this line -->
< div class = "container-fluid" >
< h3 class = "text-primary text-center" > jQuery Playground< / h3 >
< div class = "row" >
< div class = "col-xs-6" >
< h4 > #left -well</ h4 >
< div class = "well" id = "left-well" >
< button class = "btn btn-default target" id = "target1" > #target1 </ button >
< button class = "btn btn-default target" id = "target2" > #target2 </ button >
< button class = "btn btn-default target" id = "target3" > #target3 </ button >
< / div >
< / div >
< div class = "col-xs-6" >
< h4 > #right -well</ h4 >
< div class = "well" id = "right-well" >
< button class = "btn btn-default target" id = "target4" > #target4 </ button >
< button class = "btn btn-default target" id = "target5" > #target5 </ button >
< button class = "btn btn-default target" id = "target6" > #target6 </ button >
< / div >
< / div >
< / div >
< / div >
```
# --solutions--
```html
< script >
$(document).ready(function() {
$("button").addClass("animated");
$(".btn").addClass("shake");
$("#target1 ").addClass("btn-primary");
});
< / script >
<!-- Only change code above this line -->
< div class = "container-fluid" >
< h3 class = "text-primary text-center" > jQuery Playground< / h3 >
< div class = "row" >
< div class = "col-xs-6" >
< h4 > #left -well</ h4 >
< div class = "well" id = "left-well" >
< button class = "btn btn-default target" id = "target1" > #target1 </ button >
< button class = "btn btn-default target" id = "target2" > #target2 </ button >
< button class = "btn btn-default target" id = "target3" > #target3 </ button >
< / div >
< / div >
< div class = "col-xs-6" >
< h4 > #right -well</ h4 >
< div class = "well" id = "right-well" >
< button class = "btn btn-default target" id = "target4" > #target4 </ button >
< button class = "btn btn-default target" id = "target5" > #target5 </ button >
< button class = "btn btn-default target" id = "target6" > #target6 </ button >
< / div >
< / div >
< / div >
< / div >
```