TamperMonkey: add element after content in a without IDs
To clarify, I wish to add a left facing arrow to the right side of a link that is inside of a <td>
(or rather to all the <td>
in that column). The arrow will later be used for an accordion dropdown menu.
First, what method can I use to insert <a class="accordion">◀</a>
? (pure JS if possible)
And how can I have it place that on EVERY <td>
that contains the barcode number but not cells that contain other links like shipIDs?
jsfiddle.net/pshock13/fotr5p93/
<table>
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><a href="http://search.com/AAA/results?s=987654321">987654321</a></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><a href="http://search.com/AAA/results?s=123456789">123456789</a></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
in the JSFiddle is a stripped down version of what I'm working with. there are no IDs to work with and any classes used are also used on other elements that have links as well.
javascript html tampermonkey
add a comment |
To clarify, I wish to add a left facing arrow to the right side of a link that is inside of a <td>
(or rather to all the <td>
in that column). The arrow will later be used for an accordion dropdown menu.
First, what method can I use to insert <a class="accordion">◀</a>
? (pure JS if possible)
And how can I have it place that on EVERY <td>
that contains the barcode number but not cells that contain other links like shipIDs?
jsfiddle.net/pshock13/fotr5p93/
<table>
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><a href="http://search.com/AAA/results?s=987654321">987654321</a></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><a href="http://search.com/AAA/results?s=123456789">123456789</a></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
in the JSFiddle is a stripped down version of what I'm working with. there are no IDs to work with and any classes used are also used on other elements that have links as well.
javascript html tampermonkey
Do you any form of identifier on the table itself, id, class or similar? Is you HTML set in stone or can you add other attributes to cells?
– Jon P
Nov 5 at 6:02
so the table does have about three classes attached to it. However, because there is only one <tbody> on the page ever, I was able to just skip to thatvar theTableBodyRows = document.querySelectorAll("tbody tr");
– Pshock13
Nov 5 at 6:43
That could land you in trouble, should anothertbody
end up on the page. If the combination of those three classes are unique to the table that is easy enough to use inquerySelectorAll
.....querySelectorAll('table.classA.ClassB.ClassC > tbody > tr > ....etc)
– Jon P
Nov 5 at 6:47
I'll probably go with this to be safe, but for the application I'm using it for, I don't think there will ever be another table on the page.
– Pshock13
Nov 5 at 7:25
add a comment |
To clarify, I wish to add a left facing arrow to the right side of a link that is inside of a <td>
(or rather to all the <td>
in that column). The arrow will later be used for an accordion dropdown menu.
First, what method can I use to insert <a class="accordion">◀</a>
? (pure JS if possible)
And how can I have it place that on EVERY <td>
that contains the barcode number but not cells that contain other links like shipIDs?
jsfiddle.net/pshock13/fotr5p93/
<table>
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><a href="http://search.com/AAA/results?s=987654321">987654321</a></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><a href="http://search.com/AAA/results?s=123456789">123456789</a></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
in the JSFiddle is a stripped down version of what I'm working with. there are no IDs to work with and any classes used are also used on other elements that have links as well.
javascript html tampermonkey
To clarify, I wish to add a left facing arrow to the right side of a link that is inside of a <td>
(or rather to all the <td>
in that column). The arrow will later be used for an accordion dropdown menu.
First, what method can I use to insert <a class="accordion">◀</a>
? (pure JS if possible)
And how can I have it place that on EVERY <td>
that contains the barcode number but not cells that contain other links like shipIDs?
jsfiddle.net/pshock13/fotr5p93/
<table>
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><a href="http://search.com/AAA/results?s=987654321">987654321</a></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><a href="http://search.com/AAA/results?s=123456789">123456789</a></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
in the JSFiddle is a stripped down version of what I'm working with. there are no IDs to work with and any classes used are also used on other elements that have links as well.
javascript html tampermonkey
javascript html tampermonkey
edited Nov 12 at 4:25
asked Nov 5 at 5:44
Pshock13
285
285
Do you any form of identifier on the table itself, id, class or similar? Is you HTML set in stone or can you add other attributes to cells?
– Jon P
Nov 5 at 6:02
so the table does have about three classes attached to it. However, because there is only one <tbody> on the page ever, I was able to just skip to thatvar theTableBodyRows = document.querySelectorAll("tbody tr");
– Pshock13
Nov 5 at 6:43
That could land you in trouble, should anothertbody
end up on the page. If the combination of those three classes are unique to the table that is easy enough to use inquerySelectorAll
.....querySelectorAll('table.classA.ClassB.ClassC > tbody > tr > ....etc)
– Jon P
Nov 5 at 6:47
I'll probably go with this to be safe, but for the application I'm using it for, I don't think there will ever be another table on the page.
– Pshock13
Nov 5 at 7:25
add a comment |
Do you any form of identifier on the table itself, id, class or similar? Is you HTML set in stone or can you add other attributes to cells?
– Jon P
Nov 5 at 6:02
so the table does have about three classes attached to it. However, because there is only one <tbody> on the page ever, I was able to just skip to thatvar theTableBodyRows = document.querySelectorAll("tbody tr");
– Pshock13
Nov 5 at 6:43
That could land you in trouble, should anothertbody
end up on the page. If the combination of those three classes are unique to the table that is easy enough to use inquerySelectorAll
.....querySelectorAll('table.classA.ClassB.ClassC > tbody > tr > ....etc)
– Jon P
Nov 5 at 6:47
I'll probably go with this to be safe, but for the application I'm using it for, I don't think there will ever be another table on the page.
– Pshock13
Nov 5 at 7:25
Do you any form of identifier on the table itself, id, class or similar? Is you HTML set in stone or can you add other attributes to cells?
– Jon P
Nov 5 at 6:02
Do you any form of identifier on the table itself, id, class or similar? Is you HTML set in stone or can you add other attributes to cells?
– Jon P
Nov 5 at 6:02
so the table does have about three classes attached to it. However, because there is only one <tbody> on the page ever, I was able to just skip to that
var theTableBodyRows = document.querySelectorAll("tbody tr");
– Pshock13
Nov 5 at 6:43
so the table does have about three classes attached to it. However, because there is only one <tbody> on the page ever, I was able to just skip to that
var theTableBodyRows = document.querySelectorAll("tbody tr");
– Pshock13
Nov 5 at 6:43
That could land you in trouble, should another
tbody
end up on the page. If the combination of those three classes are unique to the table that is easy enough to use in querySelectorAll
..... querySelectorAll('table.classA.ClassB.ClassC > tbody > tr > ....etc)
– Jon P
Nov 5 at 6:47
That could land you in trouble, should another
tbody
end up on the page. If the combination of those three classes are unique to the table that is easy enough to use in querySelectorAll
..... querySelectorAll('table.classA.ClassB.ClassC > tbody > tr > ....etc)
– Jon P
Nov 5 at 6:47
I'll probably go with this to be safe, but for the application I'm using it for, I don't think there will ever be another table on the page.
– Pshock13
Nov 5 at 7:25
I'll probably go with this to be safe, but for the application I'm using it for, I don't think there will ever be another table on the page.
– Pshock13
Nov 5 at 7:25
add a comment |
1 Answer
1
active
oldest
votes
You can grab all the approriate cells with the querySelectorAll
knowing that the barcode is the 2nd column. Knowing that we can use the nth-child
selector.
//NEW AND IMPORVED
//Get just the cells we wan knowing barcode is the second column
var barCodeCells = document.querySelectorAll("#theTable tbody tr > td:nth-child(2) > div");
var newElement = "<a class='accordion'>◀</a>";
//Iterate our cells
for(var i = 0; i < barCodeCells.length; i++){
//Append the new element to the innerHTML
barCodeCells[i].innerHTML += newElement;
}
//Old Version
//Get the rows from the body only
/*var theTableBodyRows = document.querySelectorAll("#theTable tbody tr");
var newElement = "<a class='accordion'>◀</a>";
//Loops throug the rows
for(var i = 0; i < theTableBodyRows.length; i++){
//Knowing th barcode is the 2ns cell get that
var targetCell = theTableBodyRows[i].querySelector("td:nth-child(2)");
//Append the new element to the innerHTML
targetCell.innerHTML += newElement;
}*/
<table id="theTable">
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><div><a href="http://search.com/AAA/results?s=987654321">987654321</a></div></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
If the arrow is purely decorative, you can skip javascript and just use CSS
#theTable>tbody>tr>td:nth-child(2) a:after {
content: '25C0';
padding-left: 3px;
}
<table id="theTable">
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><div><a href="http://search.com/AAA/results?s=987654321">987654321</a></div></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
This works very well! however, the issue (my fault entirely) is that the <a> is also wrapped in a <div>. so,<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
How do I go about selecting the div inside to add my arrow? Found it:theTableBodyRows[i].querySelector("td:nth-child(2) div");
will select the div inside the 2nd cell of each row
– Pshock13
Nov 5 at 6:28
We just need to tweak the selectors. For the javascript version I've added a new div child :> div
. For the CSS version I've changed the child selector>
to descendant, which is a space.
– Jon P
Nov 5 at 6:36
I like that you included the CSS variant. though for my purpose going forward, I need this to be an element I think.
– Pshock13
Nov 5 at 7:41
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148953%2ftampermonkey-add-element-after-content-in-a-td-without-ids%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can grab all the approriate cells with the querySelectorAll
knowing that the barcode is the 2nd column. Knowing that we can use the nth-child
selector.
//NEW AND IMPORVED
//Get just the cells we wan knowing barcode is the second column
var barCodeCells = document.querySelectorAll("#theTable tbody tr > td:nth-child(2) > div");
var newElement = "<a class='accordion'>◀</a>";
//Iterate our cells
for(var i = 0; i < barCodeCells.length; i++){
//Append the new element to the innerHTML
barCodeCells[i].innerHTML += newElement;
}
//Old Version
//Get the rows from the body only
/*var theTableBodyRows = document.querySelectorAll("#theTable tbody tr");
var newElement = "<a class='accordion'>◀</a>";
//Loops throug the rows
for(var i = 0; i < theTableBodyRows.length; i++){
//Knowing th barcode is the 2ns cell get that
var targetCell = theTableBodyRows[i].querySelector("td:nth-child(2)");
//Append the new element to the innerHTML
targetCell.innerHTML += newElement;
}*/
<table id="theTable">
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><div><a href="http://search.com/AAA/results?s=987654321">987654321</a></div></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
If the arrow is purely decorative, you can skip javascript and just use CSS
#theTable>tbody>tr>td:nth-child(2) a:after {
content: '25C0';
padding-left: 3px;
}
<table id="theTable">
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><div><a href="http://search.com/AAA/results?s=987654321">987654321</a></div></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
This works very well! however, the issue (my fault entirely) is that the <a> is also wrapped in a <div>. so,<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
How do I go about selecting the div inside to add my arrow? Found it:theTableBodyRows[i].querySelector("td:nth-child(2) div");
will select the div inside the 2nd cell of each row
– Pshock13
Nov 5 at 6:28
We just need to tweak the selectors. For the javascript version I've added a new div child :> div
. For the CSS version I've changed the child selector>
to descendant, which is a space.
– Jon P
Nov 5 at 6:36
I like that you included the CSS variant. though for my purpose going forward, I need this to be an element I think.
– Pshock13
Nov 5 at 7:41
add a comment |
You can grab all the approriate cells with the querySelectorAll
knowing that the barcode is the 2nd column. Knowing that we can use the nth-child
selector.
//NEW AND IMPORVED
//Get just the cells we wan knowing barcode is the second column
var barCodeCells = document.querySelectorAll("#theTable tbody tr > td:nth-child(2) > div");
var newElement = "<a class='accordion'>◀</a>";
//Iterate our cells
for(var i = 0; i < barCodeCells.length; i++){
//Append the new element to the innerHTML
barCodeCells[i].innerHTML += newElement;
}
//Old Version
//Get the rows from the body only
/*var theTableBodyRows = document.querySelectorAll("#theTable tbody tr");
var newElement = "<a class='accordion'>◀</a>";
//Loops throug the rows
for(var i = 0; i < theTableBodyRows.length; i++){
//Knowing th barcode is the 2ns cell get that
var targetCell = theTableBodyRows[i].querySelector("td:nth-child(2)");
//Append the new element to the innerHTML
targetCell.innerHTML += newElement;
}*/
<table id="theTable">
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><div><a href="http://search.com/AAA/results?s=987654321">987654321</a></div></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
If the arrow is purely decorative, you can skip javascript and just use CSS
#theTable>tbody>tr>td:nth-child(2) a:after {
content: '25C0';
padding-left: 3px;
}
<table id="theTable">
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><div><a href="http://search.com/AAA/results?s=987654321">987654321</a></div></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
This works very well! however, the issue (my fault entirely) is that the <a> is also wrapped in a <div>. so,<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
How do I go about selecting the div inside to add my arrow? Found it:theTableBodyRows[i].querySelector("td:nth-child(2) div");
will select the div inside the 2nd cell of each row
– Pshock13
Nov 5 at 6:28
We just need to tweak the selectors. For the javascript version I've added a new div child :> div
. For the CSS version I've changed the child selector>
to descendant, which is a space.
– Jon P
Nov 5 at 6:36
I like that you included the CSS variant. though for my purpose going forward, I need this to be an element I think.
– Pshock13
Nov 5 at 7:41
add a comment |
You can grab all the approriate cells with the querySelectorAll
knowing that the barcode is the 2nd column. Knowing that we can use the nth-child
selector.
//NEW AND IMPORVED
//Get just the cells we wan knowing barcode is the second column
var barCodeCells = document.querySelectorAll("#theTable tbody tr > td:nth-child(2) > div");
var newElement = "<a class='accordion'>◀</a>";
//Iterate our cells
for(var i = 0; i < barCodeCells.length; i++){
//Append the new element to the innerHTML
barCodeCells[i].innerHTML += newElement;
}
//Old Version
//Get the rows from the body only
/*var theTableBodyRows = document.querySelectorAll("#theTable tbody tr");
var newElement = "<a class='accordion'>◀</a>";
//Loops throug the rows
for(var i = 0; i < theTableBodyRows.length; i++){
//Knowing th barcode is the 2ns cell get that
var targetCell = theTableBodyRows[i].querySelector("td:nth-child(2)");
//Append the new element to the innerHTML
targetCell.innerHTML += newElement;
}*/
<table id="theTable">
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><div><a href="http://search.com/AAA/results?s=987654321">987654321</a></div></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
If the arrow is purely decorative, you can skip javascript and just use CSS
#theTable>tbody>tr>td:nth-child(2) a:after {
content: '25C0';
padding-left: 3px;
}
<table id="theTable">
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><div><a href="http://search.com/AAA/results?s=987654321">987654321</a></div></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
You can grab all the approriate cells with the querySelectorAll
knowing that the barcode is the 2nd column. Knowing that we can use the nth-child
selector.
//NEW AND IMPORVED
//Get just the cells we wan knowing barcode is the second column
var barCodeCells = document.querySelectorAll("#theTable tbody tr > td:nth-child(2) > div");
var newElement = "<a class='accordion'>◀</a>";
//Iterate our cells
for(var i = 0; i < barCodeCells.length; i++){
//Append the new element to the innerHTML
barCodeCells[i].innerHTML += newElement;
}
//Old Version
//Get the rows from the body only
/*var theTableBodyRows = document.querySelectorAll("#theTable tbody tr");
var newElement = "<a class='accordion'>◀</a>";
//Loops throug the rows
for(var i = 0; i < theTableBodyRows.length; i++){
//Knowing th barcode is the 2ns cell get that
var targetCell = theTableBodyRows[i].querySelector("td:nth-child(2)");
//Append the new element to the innerHTML
targetCell.innerHTML += newElement;
}*/
<table id="theTable">
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><div><a href="http://search.com/AAA/results?s=987654321">987654321</a></div></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
If the arrow is purely decorative, you can skip javascript and just use CSS
#theTable>tbody>tr>td:nth-child(2) a:after {
content: '25C0';
padding-left: 3px;
}
<table id="theTable">
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><div><a href="http://search.com/AAA/results?s=987654321">987654321</a></div></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
//NEW AND IMPORVED
//Get just the cells we wan knowing barcode is the second column
var barCodeCells = document.querySelectorAll("#theTable tbody tr > td:nth-child(2) > div");
var newElement = "<a class='accordion'>◀</a>";
//Iterate our cells
for(var i = 0; i < barCodeCells.length; i++){
//Append the new element to the innerHTML
barCodeCells[i].innerHTML += newElement;
}
//Old Version
//Get the rows from the body only
/*var theTableBodyRows = document.querySelectorAll("#theTable tbody tr");
var newElement = "<a class='accordion'>◀</a>";
//Loops throug the rows
for(var i = 0; i < theTableBodyRows.length; i++){
//Knowing th barcode is the 2ns cell get that
var targetCell = theTableBodyRows[i].querySelector("td:nth-child(2)");
//Append the new element to the innerHTML
targetCell.innerHTML += newElement;
}*/
<table id="theTable">
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><div><a href="http://search.com/AAA/results?s=987654321">987654321</a></div></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
//NEW AND IMPORVED
//Get just the cells we wan knowing barcode is the second column
var barCodeCells = document.querySelectorAll("#theTable tbody tr > td:nth-child(2) > div");
var newElement = "<a class='accordion'>◀</a>";
//Iterate our cells
for(var i = 0; i < barCodeCells.length; i++){
//Append the new element to the innerHTML
barCodeCells[i].innerHTML += newElement;
}
//Old Version
//Get the rows from the body only
/*var theTableBodyRows = document.querySelectorAll("#theTable tbody tr");
var newElement = "<a class='accordion'>◀</a>";
//Loops throug the rows
for(var i = 0; i < theTableBodyRows.length; i++){
//Knowing th barcode is the 2ns cell get that
var targetCell = theTableBodyRows[i].querySelector("td:nth-child(2)");
//Append the new element to the innerHTML
targetCell.innerHTML += newElement;
}*/
<table id="theTable">
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><div><a href="http://search.com/AAA/results?s=987654321">987654321</a></div></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
#theTable>tbody>tr>td:nth-child(2) a:after {
content: '25C0';
padding-left: 3px;
}
<table id="theTable">
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><div><a href="http://search.com/AAA/results?s=987654321">987654321</a></div></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
#theTable>tbody>tr>td:nth-child(2) a:after {
content: '25C0';
padding-left: 3px;
}
<table id="theTable">
<thead>
<tr class="header">
<th class="tablesorter-header">Shipment ID</th>
<th class="tablesorter-header">Barcode</th>
<th class="tablesorter-header"> More info</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://search.com/AAA/results?s=7546895555555">7546895555555</a></td>
<td><div><a href="http://search.com/AAA/results?s=987654321">987654321</a></div></td>
<td>more stuff</td>
</tr>
<tr>
<td><a href="http://search.com/AAA/results?s=1234567222222">1234567222222</a></td>
<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
<td>other stuff</td>
</tr>
</tbody>
</table>
edited Nov 5 at 6:30
answered Nov 5 at 6:10
Jon P
11.7k73458
11.7k73458
This works very well! however, the issue (my fault entirely) is that the <a> is also wrapped in a <div>. so,<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
How do I go about selecting the div inside to add my arrow? Found it:theTableBodyRows[i].querySelector("td:nth-child(2) div");
will select the div inside the 2nd cell of each row
– Pshock13
Nov 5 at 6:28
We just need to tweak the selectors. For the javascript version I've added a new div child :> div
. For the CSS version I've changed the child selector>
to descendant, which is a space.
– Jon P
Nov 5 at 6:36
I like that you included the CSS variant. though for my purpose going forward, I need this to be an element I think.
– Pshock13
Nov 5 at 7:41
add a comment |
This works very well! however, the issue (my fault entirely) is that the <a> is also wrapped in a <div>. so,<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
How do I go about selecting the div inside to add my arrow? Found it:theTableBodyRows[i].querySelector("td:nth-child(2) div");
will select the div inside the 2nd cell of each row
– Pshock13
Nov 5 at 6:28
We just need to tweak the selectors. For the javascript version I've added a new div child :> div
. For the CSS version I've changed the child selector>
to descendant, which is a space.
– Jon P
Nov 5 at 6:36
I like that you included the CSS variant. though for my purpose going forward, I need this to be an element I think.
– Pshock13
Nov 5 at 7:41
This works very well! however, the issue (my fault entirely) is that the <a> is also wrapped in a <div>. so,
<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
How do I go about selecting the div inside to add my arrow? Found it:theTableBodyRows[i].querySelector("td:nth-child(2) div");
will select the div inside the 2nd cell of each row– Pshock13
Nov 5 at 6:28
This works very well! however, the issue (my fault entirely) is that the <a> is also wrapped in a <div>. so,
<td><div><a href="http://search.com/AAA/results?s=123456789">123456789</a></div></td>
How do I go about selecting the div inside to add my arrow? Found it:theTableBodyRows[i].querySelector("td:nth-child(2) div");
will select the div inside the 2nd cell of each row– Pshock13
Nov 5 at 6:28
We just need to tweak the selectors. For the javascript version I've added a new div child :
> div
. For the CSS version I've changed the child selector >
to descendant, which is a space.– Jon P
Nov 5 at 6:36
We just need to tweak the selectors. For the javascript version I've added a new div child :
> div
. For the CSS version I've changed the child selector >
to descendant, which is a space.– Jon P
Nov 5 at 6:36
I like that you included the CSS variant. though for my purpose going forward, I need this to be an element I think.
– Pshock13
Nov 5 at 7:41
I like that you included the CSS variant. though for my purpose going forward, I need this to be an element I think.
– Pshock13
Nov 5 at 7:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148953%2ftampermonkey-add-element-after-content-in-a-td-without-ids%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you any form of identifier on the table itself, id, class or similar? Is you HTML set in stone or can you add other attributes to cells?
– Jon P
Nov 5 at 6:02
so the table does have about three classes attached to it. However, because there is only one <tbody> on the page ever, I was able to just skip to that
var theTableBodyRows = document.querySelectorAll("tbody tr");
– Pshock13
Nov 5 at 6:43
That could land you in trouble, should another
tbody
end up on the page. If the combination of those three classes are unique to the table that is easy enough to use inquerySelectorAll
.....querySelectorAll('table.classA.ClassB.ClassC > tbody > tr > ....etc)
– Jon P
Nov 5 at 6:47
I'll probably go with this to be safe, but for the application I'm using it for, I don't think there will ever be another table on the page.
– Pshock13
Nov 5 at 7:25