We help IT Professionals succeed at work.
Get Started

jQuery getElementsByTagName not working

pzozulka asked
Last Modified: 2016-03-28
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>
    <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>
      <h1>Important People in Computer Science</h1>
      <table id="important">
                <th>First Name</th><th>Last Name</th><th>Date of Birth</th><th>Accomplishment</th>
                <td>Originated the concept of a programmable computer, invented the first mechanical computer.</td>
                <td>First computer programmer. Wrote an algorithm for Babbage's mechanical computer.</td>
                <td>Invented the Turing Machine, a hypothetical device that's a model of a general purpose computer.</td>
                <td>Invented the first compiler for a computer programming language, popularized the term "debugging" for fixing computer glitches.</td>

Open in new window

"use strict";

window.onload = function() {
    $( document ).ready(function() {

Open in new window

"use strict";

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

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.
Watch Question
Most Valuable Expert 2020
Most Valuable Expert 2018
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE