0

I am trying to wrap my head around this issue, but don't see what causes the error, I have following function that gets errors only in ie8 and due to this breaks all jQuery functionality as well as other JavaScript bits. (function is located inside app.js file)

function visitorAges(category, number) {
    // Ages for adults
    if (category == 'adult') {
        var arrayAges = ['18 - 20', '21 - 30', '31 - 40', '41 - 50', '51 - 60', '60 +'];
    }
    // Ages for children
    else if (category == 'child') {
        var arrayAges = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17];
    }
    // Populate related select menu with options    
    $('#visit_'+category+'_ages').empty();
        for (var i=1; i<=number; i++ ) {
            var $ageLabel = $('<label/>', {for: 'visit_'+category+'-'+i+'_age', text: category+' '+i});
            var $ageSelect = $('<select/>', {name: 'visit_'+category+'-'+i+'_age', id: 'visit_'+category+'-'+i+'_age', class: 'form-control'});
            $.each(arrayAges, function(index, value){
            $ageSelect.append($('<option/>', {text: value}));
            })
            $('#visit_'+category+'_ages').append($ageLabel, $ageSelect).fadeIn();
    }
} /* END visitorAges() */

The errors I get in IE8 console are as follows:

Expected identifier, string or number 'app.js, line 99 character 35'

var arrayAges = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17];

and

Expected identifier, string or number 'app.js, line 104 character 35'

var $ageLabel = $('<label/>', {for: 'visit_'+category+'-'+i+'_age', text: category+' '+i});
8
  • What version of jQuery are you targeting? Commented Mar 13, 2015 at 12:28
  • @wicker95 2.1.3 for ie9 and above, 1.11.2 for ie8 and below, I had this thought as well, but after removing problematic function mentioned in question, everything worked, thus right jQuery is loaded Commented Mar 13, 2015 at 12:30
  • Which code line does throw the error? Commented Mar 13, 2015 at 12:33
  • Anything in here useful: stackoverflow.com/questions/5584249/…? Commented Mar 13, 2015 at 12:34
  • I bet that's what it is: you have an object property called class. I reckon if you put that in quotes you'll be golden. Best off quoting all your keys for consistency. Commented Mar 13, 2015 at 12:35

2 Answers 2

1

So, as I said in my comments, IE requires keywords to be wrapped in quotes when they are not used as keywords, such as the one below. Notice the keywords for, class etc.

var $ageLabel = $('<label/>', {for: 'visit_'+category+'-'+i+'_age', text: category+' '+i});
var $ageSelect = $('<select/>', {name: 'visit_'+category+'-'+i+'_age', id: 'visit_'+category+'-'+i+'_age', class: 'form-control'});

So, all we need to do is to wrap them as below which works in all browsers.

var $ageLabel = $('<label/>', {'for': 'visit_'+category+'-'+i+'_age', 'text': category+' '+i});
var $ageSelect = $('<select/>', {'name': 'visit_'+category+'-'+i+'_age', 'id': 'visit_'+category+'-'+i+'_age', 'class': 'form-control'});

P.S. I have wrapped all keys just to be consistent.

Sign up to request clarification or add additional context in comments.

Comments

0

Mh, I guess you need String in your array, not Number:

var arrayAges = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17'];

Also, you should (not necessary but prefered) to declare once your var arrayAges before the condition and affect it in your if/else.

Comments

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.