1

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.

1
  • Use class instead. Commented Aug 22, 2016 at 15:44

4 Answers 4

2

IDs should be unique and only one element on a page should have a given ID. Trying using a class instead.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for answer.but there are have more element class with css.
1

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.

Comments

0

First thing you should not use same id for all element, id should be unique to be ever element,

you should use class in this scenario, because you want to share the same behaviour for multiple element you can achieve this by using class,

1 Comment

thank you....i got answer.i changed css for id and changed class for script.its working.... :D
0

Try to use different Id's for each element.

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.