Avatar of pzozulka
pzozulka
 asked on

jQuery getElementsByTagName not working

I'm doing an assignment for a class, where it says I need to modify jquery.altrow.js to create a plugin that uses the getElementsByTagName method to get all of an element’s “tr” child elements. They provide the following HTML and JavaScript:
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">	
    <title>Alternating Row Plugin</title>
    <link rel="stylesheet" href="altrow.css">
    <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script src="jquery.altrow.js"></script>
    <script src="altrow.js"></script>
</head>
<body>
    <main>
      <h1>Important People in Computer Science</h1>
      <table id="important">
            <tr>
                <th>First Name</th><th>Last Name</th><th>Date of Birth</th><th>Accomplishment</th>
            </tr>
            <tr>
                <td>Charles</td><td>Babbage</td><td>12/26/1791</td>
                <td>Originated the concept of a programmable computer, invented the first mechanical computer.</td>
            </tr>
            <tr>
                <td>Ada</td><td>Lovelace</td><td>12/10/1815</td>
                <td>First computer programmer. Wrote an algorithm for Babbage's mechanical computer.</td>
            </tr>
            <tr>
                <td>Alan</td><td>Turing</td><td>6/23/1912</td>
                <td>Invented the Turing Machine, a hypothetical device that's a model of a general purpose computer.</td>
            </tr>
            <tr>
                <td>Grace</td><td>Hopper</td><td>12/9/1906</td>
                <td>Invented the first compiler for a computer programming language, popularized the term "debugging" for fixing computer glitches.</td>
            </tr>
        </table>
    </main>
</body>
</html>

Open in new window

altrow.js
"use strict";

window.onload = function() {
    $( document ).ready(function() {
	    $("#important").highlightRows();
	});
};

Open in new window

jquery.altrow.js
"use strict";

(function($){
	$.fn.highlightRows = function() {
		
		var testItems = this.getElementsByTagName("tr");
	};
})(jQuery);

Open in new window


I am getting an error in Chrome's F12 tools console:

Uncaught TypeError: this.getElementsByTagName is not a function

I tried using the jQuery find method instead which works fine, but the question here is to use getElementsByTagName instead.
jQueryJavaScript

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck