jQuery getAttribute in IE8 replacement

380 views Asked by At

I am trying to get the following code to work in IE8:

jQuery:

$(".scroll, .tobottom, .video-cta").on("click",function(e){
    e.preventDefault();
    var target = "#" + $(this).getAttribute("data-target") + " h1";
    $("html, body").animate({
        scrollTop: $(target).position().top
    }, {duration: 2000, easing: "easeInOutQuint"});
});

Basically, the HTML that would be associated with something like this would be:

HTML:

<a class="scroll" href="#" data-target="videocontent">Some content</a>

Because IE8 doesn't support getAttribute, I am trying to find a way to get the data-target in all browsers. Any advice?

1

There are 1 answers

2
Scimonster On

getAttribute isn't a jQuery method - attr() is:

var target = "#" + $(this).attr("data-target") + " h1";

That should work.

Alternately, use the .data() method:

var target = "#" + $(this).data("target") + " h1";

All jQuery methods are designed to work even in old IE, at least in jQuery 1.x.