0

I'm trying to add a class to a li in jquery (or javascript). It works fine in FF and Google, but IE8 doesn't seem to recognize the code, because there's no class added.

I'm using jquery 1.9.1, you can see example here:

The thing is I'm using http://mediaelementjs.com/ to add audio (various songs) that on click is reproduced, each of these songs are wrapped in a li in javascript. That's why I need to add each li a class to assign a background-image to each song.

I tried the following, neither works:

/// first code ///

$(document).ready(function() {
    $(".mejs li").each(function(i) {
        $(this).addClass("at_" + (i+1));
    });
});

/// second code ///

$(document).ready(function(){
  var i = 1;
  $(".mejs li").each( function() {
      $(this).attr("class", "at_"+i);
      i++;
  });
});

/// third code ///

var elements = document.getElementsByTagName("li");
var nameClass = '';
for (i = 0; i < elements.length; i++) {
    nameClass =  elements[i].className+" at_"+i; 
    elements[i].className = nameClass;
}
  • This is the example link: http://mfwebs.com/cbr/web2/playlist.html – Myrna Freites Sep 05 '16 at 14:21
  • 1
    First code should run just fine, even in IE8. Maybe your problem is related ot this: http://stackoverflow.com/questions/18893361/jquery-add-and-remove-classes-not-working-ie8/19972627#19972627 ? – ninja Sep 05 '16 at 14:23
  • Are you getting any errors in the dev tools console? Also, have you confirmed that the browser isn't in Quirks mode? That would definitely break anything you do in jQuery. – Simba Sep 05 '16 at 14:33
  • 1
    JQuery 1.12 is the highest that still suppports IE8, so if ninjas suggestion is unrelated, try downgrading. Worst case, set the className yourself bymanipulating the className string. – Shilly Sep 05 '16 at 14:38
  • Hi guys, I solved the problem, IE8 wasn't in quirks mode, I changed the jquery to 1.12 like Shilly suggested but that wasn't the thing. I was using the mediaelementjs.com plugin with https://github.com/duozersk/mep-feature-playlist/blob/master/mep-feature-playlist.js and I added the first code posted inside the mep-feature-playlist.js in the part that was generating the UL LI (line 186) why it worked that way? I don't know. Thank you all for helping :) – Myrna Freites Sep 06 '16 at 20:24

0 Answers0