Question

Define function getPeopleInSales() Print the names of all the people in the sales department. <div> <table>...

Define function getPeopleInSales()

Print the names of all the people in the sales department.

<div>
<table>
<tr>
<th>Name</th>
<th>Department</th>
</tr>
<tr>
<td class="empName">John</td>
<td>Sales</td>
</tr>
<tr>
<td class="empName">Amy</td>
<td>Finance</td>
</tr>
<tr>
<td class="empName">Austin</td>
<td>Sales</td>
</tr>
<tr>
<td class="empName">Katie</td>
<td>Marketing</td>
</tr>
<tr>
<td class="empName" data-customAttr="court">Courtney</td>
<td>Sales</td>
</tr>
<tr>
<td class="empName">Scout</td>
<td>Sales</td>
</tr>
</table>
</div>

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Complete HTML + JavaScript code for the given requirement:

<!DOCTYPE html>
<html>
<head>
<title>Read Data from HTML Table uisng JavaScript</title>
</head>
<body>
   <table id="empTable">
   <tr>
       <th>Name</th>
       <th>Department</th>
   </tr>
   <tr>
       <td class="empName">John</td>
       <td>Sales</td>
   </tr>
   <tr>
       <td class="empName">Amy</td>
       <td>Finance</td>
   </tr>
   <tr>
   <td class="empName">Austin</td>
   <td>Sales</td>
   </tr>
   <tr>
   <td class="empName">Katie</td>
   <td>Marketing</td>
   </tr>
   <tr>
   <td class="empName" data-customAttr="court">Courtney</td>
   <td>Sales</td>
   </tr>
   <tr>
   <td class="empName">Scout</td>
   <td>Sales</td>
   </tr>
   </table>
</div>

<p><input type="button" id="bt" value="Show Sales Department Data" onclick="getPeopleInSales()" /></p>
<p id="info"></p>
</body>

<script>
function getPeopleInSales() {
document.getElementById('info').innerHTML = "";
var myTab = document.getElementById('empTable');

// LOOP THROUGH EACH ROW OF THE TABLE AFTER HEADER.
for (i = 1; i < myTab.rows.length; i++) {

// GET THE CELLS COLLECTION OF THE CURRENT ROW.
var objCells = myTab.rows.item(i).cells;

// LOOP THROUGH EACH CELL OF THE CURENT ROW TO READ CELL VALUES.
for (var j = 0; j < objCells.length; j++) {
              
               //get the department name
               department = objCells[1].innerText;
              
               //if department name matches Sales
               if(department == 'Sales'){
                   info.innerHTML = info.innerHTML +' ' +objCells.item(j).innerHTML;
               }
               break;
}
info.innerHTML = info.innerHTML ; // ADD A BREAK (TAG) IF NEW LINE IS REQUIRED.
}
}
</script>
</html>

Output:

Add a comment
Know the answer?
Add Answer to:
Define function getPeopleInSales() Print the names of all the people in the sales department. <div> <table>...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Only need help with question 5 2 CASE The Human Resource Function of Harrison Brothers Corporation...

    Only need help with question 5 2 CASE The Human Resource Function of Harrison Brothers Corporation COMPANY HISTORY Harrison Brothers Corporation n was founded in upstate New York on September 15, 1898, by Aubrey and William Harrison. Harrison Brothers is a multi-line traditional department store tha t cares mainly men's, wome expanded to include household furnishings and other items for the home. The long-term goal of the company is to become the leading chain of department stores in the Northeast,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT