Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (7k points)

I am trying to run a function in javascript with onclick of a button, here I am getting an error saying:

document.getElementByClass is not a function

Here is my code:

var stopMusicExt = document.getElementByClass("stopButton");
    stopButton.onclick = function() {
        var ta = document.getElementByClass("stopButton");
        document['player'].stopMusicExt(ta.value);
        ta.value = ""

    }; 

Can someone help me with this? 

1 Answer

0 votes
by (13.1k points)

There is no function named document.getElementByClass in javascript, you have to use document.getElementsByClassName() to get the first element in the resulting node( better use different names for each class), but you would still get document.getElementsByClassName() is not a function in older browsers, then you have to provide a fallback implementation if you need support of older browsers.

var stopMusicExt = document.getElementsByClassName("stopButton")[0];

 stopButton.onclick = function()

 {

var ta = document.getElementsByClassName("stopButton")[0]; document['player'].stopMusicExt(ta.value);

ta.value = ""; 

};

 Interested in full stack developer? Check out this full stack developer course from Intellipaat.

Related questions

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...