javascript - For loop doesn't work for image selector -
i'm trying make picture selector loop doesn't work.
it should make selected div orange, , turn other divs white.
function clickpic(id) { document.getelementbyid("pic"+id).style.backgroundcolor='orange'; for(var = 0; < 310; i++) { if(!i == id) { document.getelementbyid("pic"+i).style.backgroundcolor='white'; } } }
the divs turning orange when clicked, others stay orange too.
looking function , need, looks want this..
you can run working example:
function clickpic(id) { // div , set div orange var selected_div = document.getelementbyid("pic"+id); selected_div.style.backgroundcolor = 'orange'; // looping through 16 divs i've created for(var = 0; < 16; i++) { if(i != id) // changed operation { document.getelementbyid("pic"+i).style.backgroundcolor='white'; } } }
.my-div{ background-color: white; height: 30px; width: 30px; cursor: pointer; border: 1px solid #000; float:left; margin: 0 10px 10px 0 }
<div id="pic0" onclick="clickpic('0')" class="my-div"></div> <div id="pic1" onclick="clickpic('1')" class="my-div"></div> <div id="pic2" onclick="clickpic('2')" class="my-div"></div> <div id="pic3" onclick="clickpic('3')" class="my-div"></div> <div id="pic4" onclick="clickpic('4')" class="my-div"></div> <div id="pic5" onclick="clickpic('5')" class="my-div"></div> <div id="pic6" onclick="clickpic('6')" class="my-div"></div> <div id="pic7" onclick="clickpic('7')" class="my-div"></div> <div id="pic8" onclick="clickpic('8')" class="my-div"></div> <div id="pic9" onclick="clickpic('9')" class="my-div"></div> <div id="pic10" onclick="clickpic('10')" class="my-div"></div> <div id="pic11" onclick="clickpic('11')" class="my-div"></div> <div id="pic12" onclick="clickpic('12')" class="my-div"></div> <div id="pic13" onclick="clickpic('13')" class="my-div"></div> <div id="pic14" onclick="clickpic('14')" class="my-div"></div> <div id="pic15" onclick="clickpic('15')" class="my-div"></div>
hope helps!
Comments
Post a Comment