1

I try to implement the effect of changing svg images by using jQuery. My code works in the browser but it returns error in the console. I want to remove the error. What should I do?

Error

(unknown) Uncaught TypeError: a[b].target.className.indexOf is not a function

jQuery

    $(document).ready(function() {
    $(window).scroll(function(){
        var ScrollTop = parseInt($(window).scrollTop());
        if(ScrollTop > 150) {
            $(".outer-wrapper").css("border-bottom","1px solid #cdc8c7");
            $(".svg--logo").find("use").attr("xlink:href", "#billie-logo-sml");
        } else {
            $(".outer-wrapper").css("border-bottom","");
            $(".svg--logo").find("use").attr("xlink:href", "#billie-logo");
        }
    })
})

HTML

<div class="header-item">
<a class="logo" href="{{ site.baseurl }}/">
  <svg class="svg--logo">
    <title>Billie Logo</title>
    <use xlink:href="#billie-logo"/>
  </svg>
</a>

0

2 Answers 2

2

You won't be able to use jQuery here. jQuery assumes all elements to be HTML and to have interfaces accordingly. But while in HTML the Element.className interface is a string, in SVG the interface SVGElement.className is a SVGAnimatedString (which is an Object with properties baseVal and animVal).

The error is not in your code, but inside the inner workings of jQuery. I can't even tell you exactly where it is, because there is no obvious reason why jQuery would need to find out the position of a character inside the class attribute - that is what the error message seems to be about.

I can't see a good reason why you would need to use jQuery at all. Using SVG means Internet Explorer 8 isn't supported anyway, and that means you can use plain Javascript without breaking anything:

document.addEventListener("load", function() {
    window.addEventListener("scroll", function() {
        var ScrollTop = Math.round(window.pageYOffset); // window.scrollY won't work in IE9+
        if(ScrollTop > 150) {
            document.querySelector(".outer-wrapper").style["border-bottom"] = "1px solid #cdc8c7";
            document.querySelector(".svg--logo use").setAttribute("xlink:href", "#billie-logo-sml");
        } else {
            document.querySelector(".outer-wrapper").style["border-bottom"] = "";
            document.querySelector(".svg--logo use").setAttribute("xlink:href", "#billie-logo");
        }
    });
});

Note that Element.querySelector only finds the first matching element. If there are multiple elements, you need to use Element.querySelectorAll and loop over the resulting Iterable.

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

8 Comments

also this error only shown in google chrome. error does not show up in safari and firefox.
Are you sure the error is raised by this event routine? Check the calls stack closely.
Yes. it's sure. When I commented out document.querySelector("use").setAttribute("xlink:href", "#billie-logo-sml"); , no error comes up in the console.
And what does the call stack say?
it shows Error at (index):373 I use jekyll and files are compiled to index.html.
|
0

In the end, I decided to use CSS background-image as there is some issue between SVG and HTML/JavaScript. It works fine. I will show part of my code.

Javascript

  $(window).scroll(function() {
    var scrollTop = Math.round(window.pageYOffset); // window.scrollY won't work in IE9+
    var elm = document.getElementById("logo");
    if(scrollTop > 30) {
        $(".outer-wrapper").css("border-bottom","1px solid #cdc8c7");
        $(".svg--logo").addClass('small');
        $(".svg--logo").removeClass('large');
    } else {
        $(".outer-wrapper").css("border-bottom","");
        $(".svg--logo").removeClass('small');
        $(".svg--logo").addClass('large');
    }
});

CSS

.large {
  background-image: url(../../../assets/images/illus/billie_logo_large.svg);
  background-repeat: no-repeat;
  margin-top: -60px;
  margin-left: -90px;
  padding-bottom:30px;
}
.small {
  background-image: url(../../../assets/images/illus/billie_logo_small.svg);
  background-repeat: no-repeat;
}
}
}

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.