2

My function works in all browsers except ie8 and below. Can anyone tell me what the problem is and possibly how to fix it? Thanks!

var match;
var chords = 
['C','C#','D','D#','E','F','F#','G','G#','A','A#','B','C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','C'];
var chords2 = 
['C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','C','C#','D','D#','E','F','F#','G','G#','A','A#','C'];
var chordRegex = /C#|D#|F#|G#|A#|Db|Eb|Gb|Ab|Bb|C|D|E|F|G|A|B/g;

function transposeUp(x) {
    $('.chord'+x).each(function(){
        ///// initializes variables /////
        var currentChord = $(this).text(); // gatheres each object
        var output = "";
        var parts = currentChord.split(chordRegex);
        var index = 0;
        /////////////////////////////////
        while (match = chordRegex.exec(currentChord)){
            var chordIndex = chords.indexOf(match[0]);
            output += parts[index++] + chords[chordIndex+1];
        }
        output += parts[index];
        $(this).text(output);
    });
}

function transposeDown(x){
$('.chord'+x).each(function(){
    var currentChord = $(this).text(); // gatheres each object
    var output = "";
    var parts = currentChord.split(chordRegex);
    var index = 0;
    while (match = chordRegex.exec(currentChord)){
        var chordIndex = chords2.indexOf(match[0],1);
        output += parts[index++] + chords2[chordIndex-1];
    }
    output += parts[index];
    $(this).text(output);
});
}

EDIT

I just found out that it has to do with the split method as well. I just can't get it fixed. The indexOf prototype works now, but tht function still isn't working but I get an error that says chordRegex is not an object. For some reason it isn't working.

Juan Gonzales
  • 1,967
  • 2
  • 22
  • 36

2 Answers2

6

IE8 does not support Array.indexOf(). You will have to import one or write your own.

See:

Community
  • 1
  • 1
karim79
  • 339,989
  • 67
  • 413
  • 406
  • +1 This is exactly what I am looking for, but how can I implement this into my function? If you don't mind... Thanks! – Juan Gonzales Jan 15 '12 at 17:47
  • Well, just add the code at the top of the question to the top of your JS. It adds an `indexOf` method to IE8's Array prototype. *However*, since you are already using jQuery I would consider looking into `$.inArray` as @gion_13 points out. – karim79 Jan 15 '12 at 17:50
  • I still haven't gotten it working. Forgive me I am a newbee at javascript and jquery. I pasted it at the top and still no luck. @dyoo do you have any ideas, maybe a fiddle? :) – Juan Gonzales Jan 15 '12 at 20:26
  • @karim79 Thanks for the help, but I just found out that this was only one of my problems. There is something else with the functions. I got an error that says chordRegex is not an object, any other ideas? – Juan Gonzales Jan 16 '12 at 00:43
  • Thanks for leading me in the right direction, but there was a problem with split method as well, so I had to rewrite that method as well. – Juan Gonzales Jan 16 '12 at 01:50
1

If you're already using jquery, you should use the $.inArray function instead of indexOf

gion_13
  • 41,171
  • 10
  • 96
  • 108