javascript - JS not working in pc but works on JSFIddle -
the code works in jsfiddle not working when put on html file. can't find error myself.
here working link fiddle demo
and bellow code not working:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>untitled document</title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script type="text/javascript"> $('#checkbox1, #checkbox2').on('change', function () { console.log(); if ($('#checkbox1').is(':checked') && $('#checkbox2').is(':checked')) { $('#circle_2').css('background-color', '#999'); } else { $('#circle_2').css('background-color', 'transparent'); } }); </script> <style type="text/css"> #circle_2 { border:solid 1px #333; width: 100px; height: 100px; border-radius: 50%; display:inline-block; } #circle { border:solid 1px #333; width: 100px; height: 100px; border-radius: 50%; display:inline-block; } .circle_text{ text-align:center; font-family:arial, helvetica, sans-serif; font-size:37px; color:#333; font-weight:bold; } </style> </head> <body> <!-- circle 1--> <div id="position_1"> <div id="circle"> <p class="circle_text"> #1 </p> </div> </div> <!-- circle 2--> <div id="position_2"> <div id="circle_2"> <p class="circle_text"> #2 </p> </div> </div> <br/><br/> <input type="checkbox" value="1" id="checkbox1" name="checkbox1"/> answer 1 <br/> <input type="checkbox" value="1" id="checkbox2" name="checkbox2"/> answer 2 <br/> <input type="checkbox" value="1" id="checkbox3" name="a3"/> answer 3 <br/> <input type="checkbox" value="1" id="checkbox4" name="a4"/> answer 4 <br/> <input type="checkbox" value="1" id="checkbox5" name="a5"/> answer 5 <br/> <input type="checkbox" value="1" id="checkbox6" name="a6"/> answer 6 <br/> <input type="checkbox" value="1" id="checkbox7" name="a7"/> answer 7 <br/> <input type="checkbox" value="1" id="checkbox8" name="a8"/> answer eight<br/> <input type="checkbox" value="1" id="checkbox9" name="a9"/> answer 9 <br/> <input type="checkbox" value="1" id="checkbox10" name="a10"/> answer ten <br/> </body> </html>
i suspect missing load, can't figure out.
you need wrap jquery code in document ready call.
$(document).ready(function () { $('#checkbox1, #checkbox2').on('change', function () { console.log(); if ($('#checkbox1').is(':checked') && $('#checkbox2').is(':checked')) { $('#circle_2').css('background-color', '#999'); } else { $('#circle_2').css('background-color', 'transparent'); } }); });
or before closing body tag. you're executing code before elements exist in page. jsfiddle.net wraps code in document ready call automatically.
Comments
Post a Comment