0

here is my jQuery and php code but jQuery code do running never. where is problem. i learned about jquery recently.

after running this code we should have two value for drop down list (very nice and nice) and after checking check box, values should change to 'not bad' and 'bad'

<script>
var $checkbox = $("#checkbox");
var $selectMenu = $("#selectMenu");

var checkedContent = "<option>not bad</option><option>bad</option>";
var uncheckedContent = $selectMenu.html();

$checkbox.on("click", function(){
   var $this = $(this);
   if($this.is(":checked:not")){
      $selectMenu.html(checkedContent);
    } else {
     $selectMenu.html(uncheckedContent);
    }
});
</script>

<input type="checkbox" name="checkbox" id="checkbox">
<select name="test" id="selectMenu">
    <option>very good</option>
    <option>nice</option>
</select> 
sammy
  • 717
  • 4
  • 13
  • did you include jquery ?? if not http://stackoverflow.com/questions/13829667/how-to-add-jquery-to-an-html-page – Mohamed-Yousef Nov 24 '15 at 14:24
  • @Mohamed-Yousef no bro!!! i did not knew that!! – sammy Nov 24 '15 at 14:28
  • @quentin right, but for your learning I want to mention this: You are dealing with a selectBox so in order to set the options in it you need to "append" it. $("#selectMenu).append(checkedContent). – Franco Nov 27 '15 at 19:28
  • @Franco — No. Setting the innerHTML is fine. Using append instead would have a different effect. Also, I'm not sajad. – Quentin Nov 27 '15 at 19:49

1 Answers1

0

Check if you have included:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

And then put:

$(function() {
    // Your Code
});

around your javascript code.

  1. You need to include the jQuery library to be able to use it.
  2. The $(function() {//Code}); makes sure that the whole page is loaded before the javascript gets executed
KiwiJuicer
  • 1,952
  • 14
  • 28