Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative for jQuery attr() in IE?

Ok, correct me if I'm wrong, but I take it that jQuery attr() does NOT work in IE. (marked wontfix) That being the case, what is the best alternative? For example, this works everywhere but IE:

jQuery(document).ready(function($) {
    $('.airsrc').each(function() {
        var $this = $(this);
        var src = $this.attr('data-websrc');
        $this.attr('src', src);
    });
});

Update: Whoops...I realize the issue. I actually had this inside an if statement based on a CSS3 media query. Media queries that aren't natively supported in IE8 or lower. The attr() definitely works!

like image 325
ryanve Avatar asked Aug 13 '11 17:08

ryanve


3 Answers

I use attr with data-* attributes on IE all the time, I've never had a problem. Here's a live version, just tested in IE6, IE7, and IE9. Don't have my IE8 box handy, but again, I've never had a problem.

like image 67
T.J. Crowder Avatar answered Oct 03 '22 22:10

T.J. Crowder


I haven't had a problem with attr() working in IE. The description of the bug listed is:

JQuery function .attr does not works under IE, when attribute is event like .attr("onchange","alert('Hello event onchange!')"); . It is because IE does not understand this. You can check, if attribute is event, make eval function if IE.

Specifically, it has to do with events. Regular attributes shouldn't be a problem.

like image 32
rossipedia Avatar answered Oct 04 '22 00:10

rossipedia


please try this:

$this.data('websrc'); instead of $this.attr('data-websrc');

like image 28
thecodeparadox Avatar answered Oct 04 '22 00:10

thecodeparadox