Skip to content
Advertisement

Search For Text in Div

I’m trying to make a runnable console command through Chrome that searches for the word “takeID”, and then grabs the content immediately after it between = and & from a div class.

What I have so far doesn’t work because I’m very bad at JS so any help would be appreciated. Below is what I have so far:

var iframe=document.getElementsByClassName("activity activity-container-html5");
var searchValue = "takeID";
for(var i=0;i<iframe.length;i++){ if(iframe[i].innerHTML.indexOf(searchValue)>-1){}};
var subString = iframe.substring( iframe.lastIndexOf("=")+1, iframe.lastIndexOf("&"));
console.log(searchValue+"="+subString);

An example of the div class it would be searching would look like:

<div class="activity activity-container-html5" config="{example text;takeID=cd251erwera34a&amp;more example text}">

There are two issues with the code. The first issue is the searchValue posts to the console as whatever is in between the takeID, and not the actual result from searching. The second issue is that the code to search between = and & doesn’t work at all and I don’t know why. What is wrong with the code?

I just want an output that would post to the log or a popup window saying:

takeID=cd251erwera34a

EDIT:

Something else I thought of was how would you be able to just parse the div and then search for what is in between “takeID=” and “&”? I tried this but I was getting the error “Uncaught TypeError: iframe.lastIndexOf is not a function”.

var iframe=document.getElementsByClassName("activity activity-container-html5");
var subString = iframe.substring( iframe.lastIndexOf("takeId=") + 1, iframe.lastIndexOf("&") );
console.log(subString);

I looked this up and I see this is because what it is trying to process is not a string but I’m not sure why that is or how to fix it.

Advertisement

Answer

I figured out what I needed to do. Below is working code:

var iframe=document.getElementsByClassName("activity activity-container-html5");
var div = "";
for(var i=0;i < iframe.length; i++){
   div += (iframe[i].outerHTML);
}
var take = /takeID=([a-z0-9]*)&/;
var capture = div.match(take);

var matchID = capture[1];

console.log(matchID);

window.alert("takeID=" + matchID);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement