Menu
Basically I want to create a greasemonkey script that will delete a whole row <tr> </tr> based on some of the class=”namehere” in the <td>…
ex:
[code=html]<table>
<tr><td>hey</rd><td> <a href=”google.com” class=”deletethis”>google, delete me</a></td></tr>
<tr><td>y0</td><td><a href=”yahoo.com” class=”keepme”>yahoo, keepme</a></td></tr>
<tr><td>y0</td><td><a href=”ask.com” class=”deletethis2″>ask, keep me</a></td></tr>
</table>
after the script runs, id only want the yahoo row.. I tryed many thing for about a hour, but I really dont know much JS…
......based on some of the class="namehere" in the <td>
--------
<tr><td>hey</rd><td> <a href="google.com" class="deletethis">google, delete me</a></td></tr>
...
[/QUOTE]
<i>
</i>var i = 0;
var e = document.getElementsByTagName("a");
while(i++<e.length) {
if(e[i].class!="keepme") { e.parentNode.removeChild(e); }
}
[code=html]
<table>
<tr>
<td>hey</rd>
<td><a href="google.com" class="deletethismaybe">deleteme</a></td>
</tr>
<tr>
<td>yo</rd>
<td><a href="google.com" class="deletethismaybe">donotdelete</a></td>
</tr>
<tr>
<td><a href="yahoo.com" class="deletethismaybe">DeleteMeAlso</a></td>
</tr>
<tr>
<td>yo</rd>
<td><a href="google.com" class="deletethismaybe">ehKeepThis</a></td>
</tr>
</table>[/code]
class="deleteme" (which I changed to class="hello" for clarification) and its always part of <a>, while also inside a <Td> which is inside a <tr> which has many td's....[/QUOTE]
<i>
</i>e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode)
... not a logical thought. Following that, the class belongs to the window, because the TD is inside a TR which is inside a TABLE... and so on till the basic root of the WINDOW object. Try to make yourself clear all the time ?
To [B]samliew[/B]: Maybe you wanted to be:
<i>
</i>e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode)
because [B]whatsInaName[/B] said clearly that he wants to remove the row...[/QUOTE]
but would that code even work? Is that code checking:
a: <a href="yahoo.com" class="checkingthis">whatever</a>
or
b: <a href="yahoo.com" class="whatever">checkingthis</a>
edit: did not work either way.[/QUOTE]
var i = 0;
var e = document.getElementsByTagName("a");
while(i<e.length) {
if(e[i].className!="keepme") { e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode); }
i++;
}
<i>
</i><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function deleteRows(){
var str=new RegExp('deleteme','gi');
var links=document.getElementById('mytab').getElementsByTagName('a');
var i=0, a, r;
while(a=links[i++]){
if(a.firstChild&&a.firstChild.data.match(str)){
r=a.parentNode.parentNode;
r.parentNode.removeChild(r);
}
}
}
</script>
</head>
<body>
<table id="mytab">
<tr>
<td>hey</rd> <td><a href="google.com" class="deletethismaybe">deleteme</a></td>
</tr>
<tr>
<td>yo</td>
<td><a href="google.com" class="deletethismaybe">donotdelete</a></td>
</tr>
<tr>
<td>blah</td>
<td><a href="yahoo.com" class="deletethismaybe">DeleteMeAlso</a></td>
</tr>
<tr>
<td>yo</td>
<td><a href="google.com" class="deletethismaybe">ehKeepThis</a></td>
</tr>
</table>
<br>
<br>
<span style="background:#cccccc;cursor:pointer" onclick="deleteRows()">Click here to delete the rows which contain the "deleteme" string</span>
</body>
</html>
var i = 0;
var e = document.getElementsByTagName("a");
alert(e.length);
while(i++<e.length)
{
alert('I am in: ' + i);
if(e[i].className="deletememe]) { e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode); }
}
alert('DONE');[/QUOTE]
[code=html]<tr class="junk here">
<td align="left" class="tabletext">
<span><a title="" href="linkhere">text here</a></span><br />
<span class="tablesubtext">
<a href="linkhere" class="maybedelthis">delme</a> <a href="linkhere" class="maybedelthis">dontdel</a> <a href="linkhere" class="maybedelthis">dontdel</a> </span>
</td>
</tr>
<tr class="junk here">
<td align="left" class="tabletext">
<span><a title="" href="linkhere">text here</a></span>
<br />
<span class="tablesubtext">
<a href="linkhere" class="maybedelthis">dontdel</a> <a href="linkhere" class="maybedelthis">dontdel</a> </span>
</td>
</tr>[/code]
[code=html]<table class="mytable"><tr class="junk here"> <td align="left" class="tabletext"> <span><a title="" href="linkhere">text here</a></span><br /> <span class="tablesubtext"> <a href="linkhere" class="maybedelthis">delme</a> <a href="linkhere" class="maybedelthis">dontdel</a> <a href="linkhere" class="maybedelthis">dontdel</a> </span> </td> </tr> <tr class="junk here"> <td align="left" class="tabletext"> <span><a title="" href="linkhere">text here</a></span> <br /> <span class="tablesubtext"> <a href="linkhere" class="maybedelthis">dontdel</a> <a href="linkhere" class="maybedelthis">dontdel</a> </span> </td> </tr></table>[/code]
well I am going to sleep now... but for one, there is no ID on the table, but there is a class...
[/quote]
also a other reminder, I do not care about the class in the <a> I only care about the text inside it, the class is just to guide is, so it doesnt take all <a></a>
[/QUOTE]
Than give it an id, what stops you to do that?[/QUOTE]
yeah that didnt work... it like JS doesnt want to loop...
That doesnt get rid of anything. anytime e is greater then 0 it enters the loop and only does it once and never prints my final 'done'... its like a JS error.
Kor: eh, looking at that now but its sort of Chinese to me. but dont forget, this is in a grease monkey script.
==EDIT==
Here is code from the site
[code=html]<tr class="junk here">
<td align="left" class="tabletext">
<span><a title="" href="linkhere">text here</a></span><br />
<span class="tablesubtext">
<a href="linkhere" class="maybedelthis">delme</a> <a href="linkhere" class="maybedelthis">dontdel</a> <a href="linkhere" class="maybedelthis">dontdel</a> </span>
</td>
</tr>
<tr class="junk here">
<td align="left" class="tabletext">
<span><a title="" href="linkhere">text here</a></span>
<br />
<span class="tablesubtext">
<a href="linkhere" class="maybedelthis">dontdel</a> <a href="linkhere" class="maybedelthis">dontdel</a> </span>
</td>
</tr>[/code]
The first row has 3 <a>, but one of them is delme has the text, so I want the whole row gone. Since the second row doesnt have any blacklisted words, I want to keep it, etc.[/QUOTE]
[B]3) Checks whether the className for each of the links is "keepme". If no, deletes the entire row.[/B][/QUOTE]
<i>
</i>var i = 0;
var e = document.getElementsByTagName("a");
while(i<e.length) {
try{
if(e[i].className!="keepme") { e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode); }
}catch (e){}
i++;
}
Don't say it doesn't work when you don't read my instructions properly.
You have to set the class name to "keepme" for whatever link you want deleted.[/QUOTE]
[code=html]e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode);[/code]
[code=html]var i = 0;
var Error = 0
var e = document.getElementsByTagName("a");
alert(e.length);
while(i<e.length) {
try{
if(e[i].className="deleteme") { }
}catch (e){}
Error++;
i++;
}
alert("done, errors: " + Error);[/code]
[code=html]var i = 0;
var Error = 0
var e = document.getElementsByTagName("a");
alert(e.length);
while(i<e.length) {
try{
if(e[i].className=="deleteme") { }
}catch (e){}
Error++;
i++;
}
alert("done, errors: " + Error);[/code]
[code=html]var i = 0;
var Error = 0
var e = document.getElementsByTagName('a');
while(i<e.length) {
try{
if(e[i].className == 'deleteme') { e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode); }
}catch (e){ Error++;}
i++;
}
alert("Done." + e.length + ' ' + i + ' ' + Error);[/code]
[code=html]var e = document.getElementsByTagName('a');[/code]
<i>
</i>...
<tr><td><a href="">delete row</a></td></tr>
<i>
</i>var e = document.getElementsByTagName('a'), grandpa;
for(var i=0;i<e.length;i++){
grandpa=e[i].parentNode.parentNode;
if(grandpa.nodeName=='TR'){grandpa.parentNode.removeChild(grandpa)}
}
e[0].parentNode.parentNode.parentNode.removeChild(e[0].parentNode.parentNode);
var i = 0;
var e = document.getElementsByTagName("a");
while(i<e.length) {
if(e[i].className!="keepme") { e[B][i][/B].parentNode.parentNode.parentNode.removeChild(e[B][i][/B].parentNode.parentNode); }
i++;
}
0.1.9 — BETA 5.25