javascript - Trying to change id for multiple rows -


i'm trying change id multiple lines in order activate , deactivate link, works first line. there way make work on entire page?

i want change paragraphs @ same time

function changediv() {     if (document.getelementbyid("enabled")) {         document.getelementbyid("enabled").id = "disabled";     } else {                   document.getelementbyid("disabled").id = "enabled";     } }  <button type="button" onclick="changediv()">display</button> <br><br> <a href="http://www.google.com" id="disabled">this paragraph.</a><br> <a href="http://www.google.com" id="disabled">this paragraph.</a><br> <a href="http://www.google.com" id="disabled">this paragraph.</a> 

as said, id attribute of element must unique in document

the id should unique throughout scope of current document

now, solution use class group similar element, , access target elements using class, can use getelementsbyclassname() or queryselectorall() given below. have used queryselectorall() since returns non-live nodelist

function changediv() {    var els = document.queryselectorall('.disabled, .enabled');    (var = 0; < els.length; i++) {      els[i].classlist.toggle('enabled');      els[i].classlist.toggle('disabled');    }  }
.disabled {    border: 1px solid red;    padding: 5px;    display: inline-block;    margin-bottom: 5px;  }  .enabled {    border: 1px solid green;    padding: 5px;    display: inline-block;    margin-bottom: 5px;  }
<button type="button" onclick="changediv()">display</button> <br><br>  <a href="http://www.google.com" class="disabled">this paragraph.</a><br>  <a href="http://www.google.com" class="disabled">this paragraph.</a><br>  <a href="http://www.google.com" class="disabled">this paragraph.</a>


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -