Search Page For String Within ElementId

deharvy
deharvy used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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

make it

function isBlonde(styleId)
{
  var body = gwndTarget.document.getElementById(styleid).innerHTML;
  return body.indexOf("Blonde");
}

if (isBlonde('style') == -1 )
{
   alert("no blonde");
}
else
{
   alert("yes blonde");
}

Author

Commented:
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?
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Okay - since you have to work with what you've got, here's some javascript to search all td's, since we can't grab them all by id (because technically they should have unique id's):

  var isBlonde = false;
  var tdElements = document.getElementsByTagName("td");
  for (var i=0; i<tdElements.length; i++) {
	if (tdElements[i].innerHTML.indexOf("Blonde") > -1){
		isBlonde = true;
		alert(1);
	}
  }

Open in new window

Author

Commented:
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?

Author

Commented:
Works like a charm. Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial