Forum Moderators: open

Message Too Old, No Replies

How to get the string within a specific tag

The only unique factor is (2) CSS classes.

         

lexipixel

9:33 am on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I am trying to pull a string out of some HTML, (in this case the string is $99.99).

The string is generated content from cart software.

The string is always a dollar amount, (e.g. start with a dollar sign and contain a period).

It is always found in the HTML as:

<font class="dogs cat_dog">STRING_DOLLAR_VALUE</font>

It is in the 3rd <font></font> tag pair on the page.

I need to write the STRING_DOLLAR_VALUE somewhere else on the page, and am having trouble pulling it out.

Here's some of what I've tried:

var fontTags = new Array();

var fontTags = document.getElementsByTagName('font');

for (n=0; n<fontTags.length; n++){
document.write(fontTags[n].innerHTML + "<br />");
}

The problem: It reads the first font element it finds, but then dies, I need the value of fontTags[2].

I can't tell if it dies because the dollar sign or period is messing it up, (a string / quoting / interpolation problem), or if I'm missing something else.

Javascript is on my list of skills right after "eats without spilling food on self" -- and I don't do well at that one either.

Any clue to what I'm doing wrong?

Fotiman

1:23 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It dies because you are using document.write.
Once the page has loaded, a call to document.write will OVERWRITE your existing page. If you change document.write to alert, do you see multiple alert messages with one of them containing the element you're looking for?

Trace

1:28 pm on Sep 18, 2008 (gmt 0)

10+ Year Member



I just tried your code, as is, and it works as expected.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Test </title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>

<font class="dogs cat_dog">$99.99</font>

<font class="dogs cat_dog">$199.99</font>

<font class="dogs cat_dog">$299.99</font>

<font class="dogs cat_dog">$399.99</font>

<font class="dogs cat_dog">$499.99</font>

<script type="text/javascript">
var fontTags = new Array();

var fontTags = document.getElementsByTagName('font');

for (n=0; n<fontTags.length; n++){
document.write(fontTags[n].innerHTML + "<br>");
}
</script>

</body>
</html>

However, not knowing exactly what it is you are trying to accomplish, this is probably not the best method to be using. In fact, I don't think font tags should be used at all.

Fotiman

1:42 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, Trace's method works because the document.write occurs before the document has finished being "written", but in theory, it may be possible for the call to getElementsByTagName to fail in his example because the DOM hasn't finished being created (but an example like Trace's will probably work fine in all modern browsers).

lexipixel

8:21 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Makes total sense now.

I didn't get a solution, but I got the answer.

It's not finding the string I want because the JS is being called at a point in the document before the string is written out.

(The alert method never shows it -- it displays the first occurrence of the <font></font> tag and displays the innerHTML string, but never finds the other two).

The situation is; I need to fix a layout problem in a customer's cart using proprietary software. I don't have access to the source, and the only point in the software I can insert the JS is before the tag sequence appears, (either in the HEAD of the page or in an unused (text) field.

I am going to look for another place in the software to insert the JS.. I may be able to insert it into the template (footer section) with a conditional statement to only execute the JS if the document URL matches the section(s) of the site I want to affect, (the template is used site-wide, e.g.- page may be named ThisPage.asp, ThatPage.asp, or OtherPage.asp, and I only want to manipulate the appearance of "ThatPage.asp").

I've already got the "if" worked out -- now I just need to get the right DOM nodes and tags worked out so it executes and inserts in the right sequence.

Thanks. Will post back the solution when it works.

Fotiman

8:27 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ideally, you should only execute your code when the DOM is ready. You can attach your code as an event listener and listen for the window onload event, or better yet when the DOM is actually ready. Here's an example that uses the Yahoo UI Library's Event Utility to attach a listener:

<script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript">
YAHOO.util.Event.onDOMReady(function () {
// Your code here
});
</script>

This can be placed anywhere in the document because the code where I've placed "// Your code here" is only executed when the DOM is ready.

Fotiman

8:29 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, you would not use document.write in this case... instead you would use DOM methods for inserting nodes into the DOM at appropriate places.

lexipixel

11:35 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok, credit first: Trace & Foti hit it -- the problem was the order of nodes appearing in document tree, specifically that the JS was trying to find elements that were not yet part of the DOM.

The solution was to break the JS in two... I used the first part to place a DIV tag with an ID in the code at the point where I wanted to replace the content (I couldn't replace the code yet, I needed to read part of it from a latter portion of the generated HTML):

<div id="rpd"></div>

I then placed more JS in the footer of the site's template, (I've reduced it down to the relevant code here):

var fontTags = new Array();
var fontTags = document.getElementsByTagName('font');

And once it collected the info I needed, I used

document.getElementById('rpd').innerHTML = '<span style=\"color:red;\">' + fontTags[2].innerHTML + '</span><br /';

I needed to do more than color the text red, (I just used that as an example here of how to manipulate the original string -- I really needed to move it from it's original position in the HTML along with some text, graphics, links and images -- but it all worked the same way).

Done and done! It ended up being a two-step-shuffle, with an innerHTML replace that got fed another innerHTML replacement, but it was fun!

Thanks to all.