I have a table like this :
<table id="mytable"
<thead...
</thead>
<tbody>
<tr rowid="661">...</tr>
<tr rowid="68">...</tr>
<tr rowid="977">...</tr>
...
</tbody>
how can I get an array of the rowid values, in the existing order ? with jquery ?
like $(‘mytable tr’). ???
thanks.
my level of javascript being close to 0, I would be grateful to anyone helping me.
thanks.
DerkJ
(DerkJ)
November 6, 2021, 7:23pm
2
$("tr").attr("rowid").each(function(e){ $(this).att(" });
pseudocode.
see here:
https://api.jquery.com/each/
if I use
var rowIDs = $("#mytable tr").attr("rowid")
I would have an array of my rowids available ?
DerkJ
(DerkJ)
November 6, 2021, 7:29pm
4
you can iterate using the each. if you have an element that contains sub elements.
var rowIDs = $("#mytable tr").each(function () {
console.log($(this).attr('rowid'));
});
Check that in the debug (browser console)
Read a simple sample that comes close to what you need here (see answer):
javascript, jquery
do you know of any macos app that would let me test some javascript code in a simple IDE ?
with debugger ?
like Thonny does for python.
Mike_D
(Mike D)
November 6, 2021, 8:53pm
7
1 Like
DerkJ
(DerkJ)
November 6, 2021, 9:36pm
8
True but it’s already loaded by xojo if i’m not mistaken and probably as easy.
I tried this, but I don"t get what I need, I get this instead :
0 <tr rowid="661"></tr>
1 <tr rowid="68"></tr>
2 <tr rowid="977"></tr>
I don’t get the value of the rowid attribute, but the value inside the tr tag.
I would like an array with [“661”,“68”,“977”], or even better with numbers only [661,68,977]
my example table is like this :
<table id="mytable">
<thead>
</thead>
<tbody>
<tr rowid="661">line6</tr>
<tr rowid="68">line8</tr>
<tr rowid="977">line7</tr>
</tbody>
</table>
how should I proceed ?
DerkJ
(DerkJ)
November 7, 2021, 2:02pm
10
Try:
var myArr = $("#mytable tr").map(function (){
return this.getAttribute("rowid"); // returns an array
});
console.log(myArr); // copy all in console (browser)
this works the same way as DerkJ solution, and gives me the same thing I don’y want
var elem = document.querySelectorAll('tr');
gives me the inner of the tr tag
var elem = document.querySelectorAll('rowid');
gives me empty inside elem.
this works ! thanks !
even better is :
var myArr = $("#mytable tr").map(function (){
return parseInt(this.getAttribute("rowid")); // returns an array
});
so I get my array of integers instead of strings
can you explain me the difference between :
DerkJ:
$(this).attr('rowid')
and
which gives completely different results !
DerkJ
(DerkJ)
November 7, 2021, 2:22pm
13
attr is jQuery getAttrubute is native js.
Note the map() function makes it an array, not the getAttribute function.
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
1 Like
I have seen that just after sending my previous message …