PDA

View Full Version : JavaScript help - Splicing Values out of a Chunk of Text


ruggeddesign
09-28-2006, 03:59 PM
Okay, I've decided to simplify this question so that I can post it on the web.

Basically, I have retrieved a chunk of text using the innerHTML command. This is what a sample of that text would look like:


<li class="inputLabel">Delivery Type:</li><li class="inputType">dropdown</li><li class="valRoutine">3</li><li class="ofFieldset">Packaging | Delivery | Shipping</li><li class="ofSpan">0</li><li class="ifSpan">shipping</li><li class="ifValue">Shipping</li><li class="dropdownValues"><ol id="dD_deliverytype"><li>FOB Our Dock</li><li>FOB Our Dock</li><li>1 Metro Denver Delivery</li><li>1 Metro Denver Delivery</li><li>Shipping</li><li>Shipping</li></ol></li>


I know that it SHOULD be possible to access each of these <li> tags individually using the DOM - and in fact, I do just that at the beginning of my JS file - grabbing each <li>.innerHTML individually. However, for some reason I cannot use the same technique in the new function I am building later on in the page.

So basically, I have a dilemna that I am having trouble solving. I need to grab the text between the <li class="ifValue"> . . . </li> and <li class="ifSpan"> . . . </li> tags. In other words, if the code above was assigned to a variable as a string, I would need to retrieve the values of:


var ifValue = Shipping;
var ifSpan = shipping;


Is there some way that I can splice these values out of the text string?

Mr J
09-28-2006, 07:59 PM
What about something on these lines

lists=document.getElementsByTagName("LI")

for(var i=0;i<lists.length;i++){

if(lists[i].className=="ifSpan"){
ifspan=lists[i].innerHTML
}

if(lists[i].className=="ifValue"){
ifvalue=lists[i].innerHTML
}

}

alert(ifspan+"\n\n"+ifvalue)

ruggeddesign
09-28-2006, 09:32 PM
What about something on these lines

lists=document.getElementsByTagName("LI")

for(var i=0;i<lists.length;i++){

if(lists[i].className=="ifSpan"){
ifspan=lists[i].innerHTML
}

if(lists[i].className=="ifValue"){
ifvalue=lists[i].innerHTML
}

}

alert(ifspan+"\n\n"+ifvalue)

Well, I've written code very similar to that (code almost identical to that and other working pieces on the same page that I am trying to fix) but it doesn't work. I think I found out why it won't work but it's much too complicated to go into an explanation here. I found a workaround for my problem - it wasn't ideal because it required the duplication of information in those lists which seems ridiculous to me - but I was able to avoid a ton of stupid Regular Expressions that I don't even know how to write so it's all good.

Either way, thanks for providing an intelligent solution to my problem - even though it just doesn't work on that part of the page . . . which, believe me, is frustrating.