I hope my question is not too specific.
I want to use a JavaScript function (below) to fetch all divs on an HTML page. The divs are not completely known beforehand, as the end changes based on line numbers and position. I can't seem to get querySelector('[id^= ...]')
to work with variables, so need to use elementIds. The function should
- grab all DIVs,
- use a regex to match the beginning of the ID (that part I know),
- make an array, and
- grab the one value I need.
I fetch the textContent and run an operation on it (also not working). I have probably made this too complicated.
I have supplied some dummy data, which works for me to a point, but not all of it. When I use a live page, I get an HTML Collection, so I have tried: e.g.:
if (targetelements) {
if (typeof targetelements.length === "undefined") {
targetelements = targetelements.textContent;
}
targetelements = [].slice.call(targetelements).map(function(el) {
return el.textContent;}).join(", ");
}
return targetelements;
}
but to no avail.
Where I have looked:
How can get the text of a div tag using only javascript (no jQuery)
JavaScript match against array
javascript - match string against the array of regular expressions
But I just have pieces I can't fit together. I basically CANNOT process the page, and CANNOT perform the other action on the argument "subfield" either.
What I am doing so wrong? Can anyone help? No jQuery (unfortunately), please. Thanks in advance.
NOTE 2 things:
- In reality, the IDs I want do not have classNames either, so I can't get the className and get the .id, and
- My function is triggered by a button and there are several iframes on the page. These are commented out below, but that's an implementation issue.
If I want a field, I put in e.g.: getfield("300", "a")
. Here is my code:
function getfield(field, subfield) {
//var iFrame = document.getElementById("iframe1").contentWindow.document.getElementById("iframe2");
var i;
var gotfield;
//var target = "standard.textArea." + field;
var target = new RegExp("standard.textArea." + field);
var targetids = "";
var targetid = "";
var val = "";
var targetelements = "";
// dummy data, I turn it on, I turn if off
var targetelements = "<div id='standard.textArea.245.Left.10'><div class='classlabel'><h2>Lorem Ipsum</h2> <p>‡a 1 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p></div><div id='standard.textArea.264.Left.11'><div class='classlabel'><h2>Lorem Ipsum</h2> <p>‡a 2 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p></div><div id='standard.textArea.300.Left.12'><div class='classlabel'><h2>Lorem Ipsum</h2> <p>‡a 3 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p></div><div id='standard.textArea.500.Left.13'><div class='classlabel'><h2>Lorem Ipsum</h2> <p>‡a 4 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p></div><div id='standard.textArea.520.Left.14'><div class='classlabel'><h2>Lorem Ipsum</h2> <p>‡a 5 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p></div>";
//targetelements = document.getElementsByTagName("div"); //<-- this is the live data I really will use, an html collection (inside an iframe.contentWindow)
targetids = targetelements.match(/id='(.*?)'/g).map((val) => {
if (val.indexOf(target) !== 1 && val.match(target)) {
return val.replace(/id='/g, '').replace("'", '');
}
})
targetid = targetids.find(el => el.match(field));
console.log("The div I want is: " + targetid);
if (document.getElementById(targetid)) {
gotfield = document.getElementById(targetid).getElementsByClassName("classlabel")[0].textContent;
if (typeof subfield === "undefined") {
// oh no
}
else {
if (gotfield.indexOf("‡" + subfield) !== -1) {
gotfield = gotfield.split("‡" + subfield + " ").pop(0).split(" ‡").shift();
}
else {
return;
}
}
return gotfield;
}
else {
return "null"; // just so I can see
}
}
// run a test with function
getfield("245", "a");