/    Sign up×
Community /Pin to ProfileBookmark

Deleting rows based on class attributes in column

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>[/code]

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…

to post a comment
JavaScript

27 Comments(s)

Copy linkTweet thisAlerts:
@KorOct 26.2007 — ...

...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]

Sounds confusing. the class should be the class of whom? The TD element? The A element?
Copy linkTweet thisAlerts:
@samliewOct 26.2007 — Here's my input:
<i>
</i>var i = 0;
var e = document.getElementsByTagName("a");
while(i++&lt;e.length) {
if(e[i].class!="keepme") { e.parentNode.removeChild(e); }
}
Copy linkTweet thisAlerts:
@whatsInaNameauthorOct 26.2007 — 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....

I originally described it wrong... Basically I want to check the text inside an <a> that has a class="hello"

ex

[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]


To sum it up: If a column (td) has any <a>'s with class that equals "deletethismaybe" and the text inside the <a></a>is the text that I do not want to see, I want the whole row gone.
Copy linkTweet thisAlerts:
@samliewOct 26.2007 — Oops, we posted at the same time.
Copy linkTweet thisAlerts:
@KorOct 26.2007 — 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]
... 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...
Copy linkTweet thisAlerts:
@whatsInaNameauthorOct 26.2007 — 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.
Copy linkTweet thisAlerts:
@samliewOct 26.2007 — ... 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]

Oh yea, in that case it would be that.

The script assumes that you want to process all the links in that page.
Copy linkTweet thisAlerts:
@whatsInaNameauthorOct 26.2007 — well tnx, but neither of them work.

never enters the if statement (or the while loop).

but for some weird reason my second different alert prints twice even thought it out of the loop.

edit: lol wtf, if I put the first alert before the closing braces of the while statement but after the closing braces of the if statement if doesnt alert, but if its before the if but after the opening braces of the while it does it...
Copy linkTweet thisAlerts:
@samliewOct 26.2007 — 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]

Let me explain. The code I posted:
var i = 0;
var e = document.getElementsByTagName("a");
while(i&lt;e.length) {
if(e[i].className!="keepme") { e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode); }
i++;
}


1) Gets all links in the page.

2) Goes through all the links.

3) Checks whether the className for each of the links is "keepme". If no, deletes the entire row.
Copy linkTweet thisAlerts:
@KorOct 26.2007 — Like this?
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/javascript"&gt;
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&amp;&amp;a.firstChild.data.match(str)){
r=a.parentNode.parentNode;
r.parentNode.removeChild(r);
}
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table id="mytab"&gt;
&lt;tr&gt;
&lt;td&gt;hey&lt;/rd&gt; &lt;td&gt;&lt;a href="google.com" class="deletethismaybe"&gt;deleteme&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;yo&lt;/td&gt;
&lt;td&gt;&lt;a href="google.com" class="deletethismaybe"&gt;donotdelete&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;blah&lt;/td&gt;
&lt;td&gt;&lt;a href="yahoo.com" class="deletethismaybe"&gt;DeleteMeAlso&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;yo&lt;/td&gt;
&lt;td&gt;&lt;a href="google.com" class="deletethismaybe"&gt;ehKeepThis&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="background:#cccccc;cursor:pointer" onclick="deleteRows()"&gt;Click here to delete the rows which contain the "deleteme" string&lt;/span&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@whatsInaNameauthorOct 26.2007 — yeah that didnt work... it like JS doesnt want to loop...

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]




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.
Copy linkTweet thisAlerts:
@whatsInaNameauthorOct 26.2007 — kor: that is to advanced, cant makes heads of it.

well I updated my last post with the exact code, I also found that the following line is causing a JS error of some sort

[htm] e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode);[/code]
Copy linkTweet thisAlerts:
@whatsInaNameauthorOct 26.2007 — well, im sick and tired of this of this makeshift "languages"... been many hours without sleep, if you guys cant figure it out, there no way ill find the secret answer.... going to bed now...
Copy linkTweet thisAlerts:
@KorOct 26.2007 — I can not do more than to give you a valid solution. My code works. If you want other conditions to remove the rows, just tell me. Also, if you don't understand something in my code, feel free to ask
Copy linkTweet thisAlerts:
@whatsInaNameauthorOct 26.2007 — well I am going to sleep now... but for one, there is no ID on the table, but there is a class...

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>

The following is the basic site: (posted here: http://www.webdeveloper.com/forum/showpost.php?p=817781&postcount=12)
[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]
Copy linkTweet thisAlerts:
@KorOct 26.2007 — well I am going to sleep now... but for one, there is no ID on the table, but there is a class...
[/quote]

Than give it an id, what stops you to do that?

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]

That is what I've already presumed in my last code, and that is exactly how my code works, seeks for a certain substring (in case insensitive) in a link and when found it, deletes the whole row.

I don't see what else you need
Copy linkTweet thisAlerts:
@whatsInaNameauthorOct 26.2007 — Than give it an id, what stops you to do that?[/QUOTE]

This is not my code, its a third party code that I am trying to alter using the firefox extension "greasemonkey".
Copy linkTweet thisAlerts:
@samliewOct 27.2007 — 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]

Don't say it doesn't work when you don't read my instructions properly.

[B]3) Checks whether the className for each of the links is "keepme". If no, deletes the entire row.[/B][/QUOTE]

You have to set the class name to "keepme" for whatever link you want deleted.
Copy linkTweet thisAlerts:
@samliewOct 27.2007 — Ok, one more shot at it, since you say it keeps getting you errors.

<i>
</i>var i = 0;
var e = document.getElementsByTagName("a");
while(i&lt;e.length) {
try{
if(e[i].className!="keepme") { e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode); }
}catch (e){}
i++;
}


If it doesn't work for you then just give it up. Anyway it's for your personal use only, and we're not obliged to follow up.
Copy linkTweet thisAlerts:
@whatsInaNameauthorOct 27.2007 — 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]


I tried that... since EVERY instance of class inside a <a> is "maybedelthis" I told it delete all of them, but it did not delete any of them... I then tried to play around with the line, but did not have any luck

[code=html]e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode);[/code]
Copy linkTweet thisAlerts:
@whatsInaNameauthorOct 27.2007 — well, it turns out I was right about the loop... because it fails everytime (the catch part is always run the same amonut of times as the length of e)

[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]


That doesn't work if the class has "deleteme" or if the text inside the <a> has "deleteme". It also doesnt work if I add the not operator.
Copy linkTweet thisAlerts:
@whatsInaNameauthorOct 27.2007 — also you are using the assignment operator, you need the comparison operator

[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]


edit: ARG, I HATE THIS... JS catching exception no matter what I do... I can just put a increment operator there with a variable in a try catch block and it will still just keep throwing exceptions.

edit2: I knew JS wasnt that screwed, that ONE time it was my mistake give me a minute.
Copy linkTweet thisAlerts:
@whatsInaNameauthorOct 27.2007 — ok, here is the update

[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]


That code will throw exceptions every time deleteme is found, so the code inside the if statement is invalid.... BUT, like I said... This is not what I need... I need to check the text within the <a> </a>...

but I am glade after 3 hours of head smashing stress I finally got something to partioally work that I dont need :eek:

But I do believe there must be some way to alter that to get the info inside the <a>....

so, I dont know what to do next.

EDIT: OK, so yeah... looks like I might have figured out a way to make the code above work IF removing the row code can work...

edit2: this is how it goes

TABLE

--ROW

---column that might delete previous row

--ROW

---column that might delete previous row

--ROW

---column that might delete previous row
Copy linkTweet thisAlerts:
@whatsInaNameauthorOct 28.2007 — bump, still looking for a solution to delete the rows...

right now I can detected them by there specific URL (not the best way, but whatever), but I still need a way to delete the row.
Copy linkTweet thisAlerts:
@whatsInaNameauthorOct 30.2007 — bump again...

Is this impossible or something? Seems like such a simple thing.

I just need to know how to delete a row after I do this
[code=html]var e = document.getElementsByTagName('a');[/code]
Copy linkTweet thisAlerts:
@KorOct 30.2007 — if your HTML structure is
<i>
</i>...
&lt;tr&gt;&lt;td&gt;&lt;a href=""&gt;delete row&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;

should be
<i>
</i>var e = document.getElementsByTagName('a'), grandpa;
for(var i=0;i&lt;e.length;i++){
grandpa=e[i].parentNode.parentNode;
if(grandpa.nodeName=='TR'){grandpa.parentNode.removeChild(grandpa)}
}
Copy linkTweet thisAlerts:
@samliewOct 30.2007 — 1) Regarding post #23:

The operator '!=' is not an assignment operator. It checks whether is NOT "keepme". I am careful not to use any assignment operators in my conditional statements. Are you following someone else's code?

2) What about showing us your entire page code, or posting a link to it?

3) To delete a row after getting it using "var e = document.getElementsByTagName('a');",
e[0].parentNode.parentNode.parentNode.removeChild(e[0].parentNode.parentNode);

4) With this, I conclude I made a simple mistake. Here's the updated code:
var i = 0;
var e = document.getElementsByTagName("a");
while(i&lt;e.length) {
if(e[i].className!="keepme") { e[B][i][/B].parentNode.parentNode.parentNode.removeChild(e[B][i][/B].parentNode.parentNode); }
i++;
}
×

Success!

Help @whatsInaName spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...