Sasindu Jayampathi Sasindu Jayampathi - 3 months ago 10
Ajax Question

how i change css with same element id for multiple element

I have multiple div on same id,
and i want change that all div backgorund.

function myfun(id) {
$('#pinsss').css({ background: "#0066ff"} );
}


this script working well to first element and others not working.

Answer

note : keep in mind, id is unique. one element can have one id and it should be different from others. instead of id, if you can use class. because , class can use for any number of elements.

#one{
  width:100px;
  height:100px;
  margin:50px;
  border:4px solid orange;
  }

#two{
  width:100px;
  height:100px;
  margin:50px;
  border:8px solid red;
  }


#three{
  width:100px;
  height:100px;
  margin:50px;
  border:6px solid blue;
  }


.all{
  background-color:pink;
  }
<html>
  <head>
  <title></title>
  </head>
  
  <div class="all" id="one"></div>
  <div class="all" id="two"></div>
  <div class="all" id="three"></div>
  

  <body>
  </body>
</html>

like above code, each div has unique id.but same class, if you do any changes to the class, that will affect to all, which has same class, name. but id is different, styles apply only for the declared id.