0

I have a HTML table and want to show and hide class for multiple div separate only with javascript. This table will be generated from PHP script there will like 1000 tag.

I hope a smart javascript function can do this job:

function showiteam() {
  document.querySelector(".zoom").style.display = "block";
}

function hideiteam(x) {
  document.querySelector(".zoom").style.display = "none";
}
#customers td,
#customers th {
  border: 1px solid #ddd;
  padding: 8px;
}

.zoom {
  display: none;
}
<table id="customers">
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  
  <tr>
    <td onmouseover="showiteam()" onmouseout="hideiteam()">
      Google
      <div class="zoom">This Testing text</div>
    </td>
    <td>Yahoo</td>
    <td>Facebook</td>
  </tr>

  <tr>
    <td onmouseover="showiteam()" onmouseout="hideiteam()">
      Google
      <div class="zoom">This Testing text</div>
    </td>
    <td>Yahoo</td>
    <td>Facebook</td>
  </tr>
</table>

3
  • What specifically are you asking? What isn't working as expected? Commented Jun 11, 2020 at 19:12
  • i need this <div class="zoom"> show and hide when mousover td Commented Jun 11, 2020 at 19:16
  • and i need that Separate for etch coulm Commented Jun 11, 2020 at 19:17

3 Answers 3

2

The call to querySelector returns only the first matched element. You can use querySelectorAll to match multiple elements. However, you probably don't want to show/hide all of the .zoom elements when hovering over any one table cell.

Instead, pass a reference to the table cell that's invoking the mouseover and mouseout events in the HTML, and call querySelector on that element so you're only matching the .zoom element contained within that cell:

function showiteam(cell) {
  cell.querySelector(".zoom").style.display = "block";
}

function hideiteam(cell) {
  cell.querySelector(".zoom").style.display = "none";
}
#customers td,
#customers th {
  border: 1px solid #ddd;
  padding: 8px;
}

.zoom {
  display: none;
}
<table id="customers">
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td onmouseover="showiteam(this)" onmouseout="hideiteam(this)">
      Google
      <div class="zoom">This Testing text</div>
    </td>
    <td>Yahoo</td>
    <td>Facebook</td>
  </tr>
  <tr>
    <td onmouseover="showiteam(this)" onmouseout="hideiteam(this)">
      Google
      <div class="zoom">This Testing text</div>
    </td>
    <td>Yahoo</td>
    <td>Facebook</td>
  </tr>
</table>

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

1 Comment

Excellent, excellent. Thank you for understanding what I mean. Thank you David
1

Not sure if I understand correct what you 're asking, but this is what I would do

// in html
<td  onmouseover="showiteam()" onmouseout="hideiteam()"> 
     Google
     <div class="zoom is-hidden">This Testing text</div>
</td>

// in css (just replace .zoom with .is-hidden)
.is-hidden {
    display: none;
}

// in js
function showiteam(){
   document.querySelector(".zoom").classList.remove("is-hidden");
}

function hideiteam(x){
    document.querySelector(".zoom").classList.add("is-hidden");
}

1 Comment

this code only show and i hide only one item in table td and want to be all and Separate
0

First, I'll say that a CSS solution would be easier, so I'll include that below.

But, since you specified Javascript in your question, I'll answer your question directly. The problem is that your querySelector is on the whole document, not on the event.target. So pure javascript fix is:

event.target.querySelector(".zoom").style.display = "block";

However, I really think the better solution would be to remove your javascript altogether, put the zoom class on the td and add this CSS definition:

.zoom:hover div {display:block}

That's a lot tidier, if you can do it.

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.