0

I have a checkbox set to checked:

<input type="checkbox" value="24hrs" name="timeformat[]" id="isCheckedFormat" checked>

<select class="form-control" name="Mon1" id="mon1"></select>

I try to do a JQuery so that if it's checked it uses an array with certain values and if it's not checked than with different array values.

if ($('#isCheckedFormat').is(':checked')true){ 
    var vals = [1,2,3,4,5];           
}else{
     var vals = ['a','b','c','d','e'];    
 }

These then populate select fields:

for(var i = 0; i<vals.length; i++) {
$('#mon1').append('<option val="'+vals[i]+'">'+vals[i]+'</option>'); 
}

At the moment the 'select option' doesn't get populated by either values from the array vals

JSFiddle

5
  • Other than the typo which @Milind mentioned - you say "These then populate select fields" - where are you doing this? Seems that you've closed the change function. Commented Aug 20, 2014 at 15:32
  • I've updated this and the select is in the html code Commented Aug 20, 2014 at 15:40
  • 1
    so you still have the problem after fixing the typo? Commented Aug 20, 2014 at 15:40
  • Yes they are still not being populates as in the jsfiddle Commented Aug 20, 2014 at 15:41
  • See the edit in my answer - updated your fiddle. Commented Aug 20, 2014 at 15:54

2 Answers 2

3

You have a type in if statement. use:

if($('#isCheckedFormat').is(':checked'))
Sign up to request clarification or add additional context in comments.

Comments

1

Be careful with the scope of your vals array. see: http://jsfiddle.net/52jw5Lpv/7/

You are having a problem since you are defining the array inside the if statement, which means it doesn't exist outside it. See my link for a fix. Note that the array/var has been defined before the if statement.

Also - note that on the initial load, the values aren't currently reflected - so you'll need to sort that. Also - Multiple check/uncheck will continually append the values, so that needs fixed aswell. (Guess these are separate issues which you should be able to sort)

2 Comments

thanks. I've updated the JSFiddle for the other fixes to. jsfiddle.net/52jw5Lpv/24
No problem. Nice one!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.