javascript - HTML/CSS/JS Want to colour group of objects and then re-colour one of them? -


basically have 6 labels acting buttons. when clicked on, change white blue. click on label, blue label turns white again , click label turns blue.

at moment code below sets them white doesnt appear colour newly clicked label blue. if comment out line:

document.getelementsbytagname('label').style.backgroundcolor = '#fff';

then each label clicked on turns blue, remains blue when click on new label. need know how set label class background white, turn background of clicked label blue.

the result should each time clicked label background should blue , others white.

thanks in advance

<script = "text\javascript">         function toggle(label) {             document.getelementbyid('one').style.display = 'block';             document.getelementsbytagname('label').style.backgroundcolor = '#fff';             document.getelementbyid(label).style.color = 'rgb(54, 95, 145)';             document.getelementbyid(label).style.backgroundcolor = 'rgb(193,203,225)';         }      </script>      <label id='label6' class='button' onmousedown = 'toggle("label6")'>personal details</label>     <label id='label1' class='button' onmousedown = 'toggle("label1")'>education</label>     <label id='label2' class='button' onmousedown = 'toggle("label2")'>achievements</label>     <label id='label3' class='button' onmousedown = 'toggle("label3")'>work experience  </label>     <label id='label5' class='button' onmousedown = 'toggle("label5")'>it skills</label>     <label id='label4' class='button' onmousedown = 'toggle("label4")'>languages</label> 

the line you've highlighted incorrect. call:

document.getelementsbytagname('label'); 

returns array of elements, array doesn't have property style. need loop through results:

var els = document.getelementsbytagname('label'); (var i=0; i<els.length; i++) {     els[i].style.backgroundcolor = '#fff'; } 

alternatively, use jquery mahesh suggests.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -