How to remove td without any identifier

enter image description here

Hi, I want to delete "Have you served in the army?" and β€œNo” if β€œAnswer” is β€œNo”, but when it will be β€œYes”, than it should show.

No matter what I tried, but it doesn't work

<script type="text/javascript"> (function(){ for(i = 0; (a = document.getElementsByTagName("td")[i]); i++){ if(document.getElementsByTagName("span")[i].innerHTML.indexOf('Have you served in the military') > -1){ document.getElementsByTagName("td")[i].style.display = "none"; } } })(); </script> 
+6
source share
3 answers

You can get all td elements, as you already did, and get span elements inside them:

 var tds = document.getElementsByTagName('TD'); for (var i = 0, l = tds.length; i != l; ++i) { var spans = tds[i].getElementsByTagName('SPAN'); for (var j = 0, l2 = spans.length; j != l2; ++j) { var span = spans[j]; if ((span.textContent = span.innerText).indexOf('Have you served in the military') != -1) { span.style.display = 'none'; break; } } } 

EDIT: OP only wants to remove the range if there is a td with the content β€œNo” (also remove the td element)

 var tds = document.getElementsByTagName('TD'); var tdsLength = tds.length; var answerNoFound = false; for (var i = 0; i != tdsLength; ++i) { var td = tds[i]; if ((td.textContent = td.innerText) == 'No') { td.style.display = 'none'; answerNoFound = true; break; } } if (answerNoFound) for (var i = 0; i != tdsLength; ++i) { var spanFound = false; var spans = tds[i].getElementsByTagName('SPAN'); for (var j = 0, l = spans.length; j != l; ++j) { var span = spans[j]; if ((span.textContent = span.innerText).indexOf('Have you served in the military') != -1) { span.style.display = 'none'; spanFound = true; break; } } if (spanFound) break; } 
0
source

You can use a child element or just replace or even hide and show.

0
source

It seems that you have a form of application and document , probably has more gaps, some outside elements td , so you do not get the right choice spans versus td .

Therefore, when you compare span content, it is most likely not a span that is inside your td loop.

 <script type="text/javascript"> (function(){ for(i = 0; (a = document.getElementsByTagName("td")[i]); i++){ if(a.getElementsByTagName("span")[0].innerHTML.indexOf('Have you served in the military') > -1){ a.style.display = "none"; } } })(); </script> 

I modified the if statement to select a span inside your td loop that should do this.

0
source

All Articles