Avatar of deharvy
deharvy
 asked on

Search Page For String Within ElementId

I need javascript code that will search through a particular elementid and then search for a string value within the text.

Here is an example of the page content:

<td id=attribute>Hair:</td><td  id=style>Blonde</td>
<td id=attribute>Eyes:</td><td  id=style>Brown</td>
<td id=attribute>Ht:</td><td  id=style>4'11"</td>
<td id=attribute>Wt:</td><td  id=style>130</td>

-----------------

Here was my failed attempt:
var isBlonde = false;
		var body = gwndTarget.document.getElementById('style');
		for (var i=0; i<body.length; i++) {
  		if (body[i].indexOf("Blonde") > -1){
  			isBlonde = true;
  			alert(1);
  		}
		}

Open in new window

JavaScriptHTML

Avatar of undefined
Last Comment
deharvy

8/22/2022 - Mon
jordanrynard

Would you consider using a framework like jQuery for your javascript operations?
It'd make your code much less bulky, if you plan on doing alot of tasks such as this.
Let me know, otherwise I can post a plain javascript answer.

Also, id's must be unique, so you can only use one instance of an individual id name (any attempt at javascript on your html would fail for this reason).
For classification of elements of the same name, use the class attribute instead of id:
For example:
<td class=attribute>Hair:</td><td class=style>Blonde</td>
<td class=attribute>Eyes:</td><td class=style>Brown</td>
<td class=attribute>Ht:</td><td class=style>4'11"</td>
<td class=attribute>Wt:</td><td class=style>130</td>

Open in new window

SOLUTION
Gurvinder Pal Singh

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
deharvy

ASKER
jordanrynard,

I'm not in charge of the source code, just trying to code a page that reads the data. In this case, I need to use javascript.

What if I ignore the element ids and just search the entire page for string values such as ">Blonde<"?

Is that possible?
ASKER CERTIFIED SOLUTION
jordanrynard

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
deharvy

ASKER
gurvinder372,

This only grabs the value of the first elementId; which goes back to what jordanrynard was saying about the elementids being unique.

In the event that I can't get unique values, is there an alternate solution?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
deharvy

ASKER
Works like a charm. Thanks!