-2

I want to select the div nameMachine using this.but I can't find how to select it: enter image description here

I can get this element with this code :

$(".switch").each(function(){
   console.log(this);
   $(this).css({
      'background-color': 'white',
      'border-style': 'solid',
      'border-color': 'inherit',
      'border-width': '3px',
   });
});
M Hamza Javed
  • 1,269
  • 4
  • 17
  • 31
Jerome
  • 1,162
  • 2
  • 16
  • 32

4 Answers4

0

Try this:

$(".switch").each(function(){
   console.log(this);
   $(this).find('#nameMachine').css({
      'background-color': 'white',
      'border-style': 'solid',
      'border-color': 'inherit',
      'border-width': '3px',
   });
});
M Hamza Javed
  • 1,269
  • 4
  • 17
  • 31
Hajji Tarik
  • 1,072
  • 7
  • 23
0

You can use find()

$(".switch").each(function(){
   console.log(this);
   $(this).find('#nameMachine').css({
      'background-color': 'white',
      'border-style': 'solid',
      'border-color': 'inherit',
      'border-width': '3px',
   });
});

You can use children() as well, but then you would need to go through those children and search for the one with the id nameMachine

Third, you can use first(), this will take the first child. However, this one is a little tricky since you have to make sure that your wanted div is always first.

Nicholas
  • 3,529
  • 2
  • 23
  • 31
0
$(".switch").each(function(){
     console.log(this);
     $(this).find('#nameMachine').css({
          'background-color': 'white',
          'border-style': 'solid',
          'border-color': 'inherit',
          'border-width': '3px',
     });
 });

or

$(".switch").each(function(){
     console.log(this);
     this.querySelector('#nameMachine').css({
          'background-color': 'white',
          'border-style': 'solid',
          'border-color': 'inherit',
          'border-width': '3px',
     });
 });
oboshto
  • 3,478
  • 4
  • 25
  • 24
0

try this

this.querySelector('#nameMachine');
Chinito
  • 1,085
  • 1
  • 9
  • 11