Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi. I have this JS code below. I 1

Status
Not open for further replies.

JBorges

Technical User
Jan 12, 2009
57
DK
Hi.

I have this JS code below. It extracts the text from my bs element. However, some of the text in the bs elements have HTML italics tag, but the code excludes the text in the italics tags while extracting the rest. How can I edit the code to also include the text in the italics tag?

HTML:
<bs id="27:46" onClick="getText(document.getElementById('vs1031'));">
    <b> 46</b></bs>
    <vs id="vs1031">About the ninth hour Jesus called out with a loud voice, saying: <i>“E´li, E´li, la´ma sa·bach·tha´ni?” that is,</i> “My God, my God, why have you forsaken me?”</vs>

Code:
function getText(what) {

    var result =''

    for (var i = 0, l = what.childNodes.length; i < l; i++)

        if (what.childNodes[i].nodeType == Node.TEXT_NODE)
            
            result += what.childNodes[i].textContent

            return result.replace(/\s+/g, ' ')

}
 
Hi

Just call getText() recursively for the child elements :
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]getText[/color][teal]([/teal]what[teal])[/teal]
[teal]{[/teal]
    [b]var[/b] result [teal]=[/teal] [green][i]''[/i][/green]

    [b]for[/b] [teal]([/teal][b]var[/b] i [teal]=[/teal] [purple]0[/purple][teal],[/teal] l [teal]=[/teal] what[teal].[/teal]childNodes[teal].[/teal]length[teal];[/teal] i [teal]<[/teal] l[teal];[/teal] i[teal]++)[/teal]
        [b]if[/b] [teal]([/teal]what[teal].[/teal]childNodes[teal][[/teal]i[teal]].[/teal]nodeType [teal]==[/teal] Node[teal].[/teal]TEXT_NODE[teal])[/teal]
            result [teal]+=[/teal] what[teal].[/teal]childNodes[teal][[/teal]i[teal]].[/teal]textContent
        [highlight][b]else[/b] [b]if[/b] [teal]([/teal]what[teal].[/teal]childNodes[teal][[/teal]i[teal]].[/teal]nodeType [teal]==[/teal] Node[teal].[/teal]ELEMENT_NODE[teal])[/teal][/highlight]
            [highlight]result [teal]+=[/teal] [COLOR=darkgoldenrod]getText[/color][teal]([/teal]what[teal].[/teal]childNodes[teal][[/teal]i[teal]])[/teal][/highlight]

    [b]return[/b] result[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/\s+/g[/fuchsia][teal],[/teal] [green][i]' '[/i][/green][teal])[/teal]
[teal]}[/teal]


Feherke.
[link feherke.github.com/][/url]
 
Thanks Feherke. That worked!

Just a another question: in the vs tag there are sometimes included a plus sign for a cross reference. I don't want to include that in the extraction. It is doing that now. I thought the original code in my first post would handle that and not include the plus sign. How would that be accomplished?

HTML:
<bs id="27:46" onClick="getText(document.getElementById('vs1031'));">
    <b> 46</b></bs>
	<vs id="vs1031">Ved den niende time råbte Jesus med høj røst og sagde: <i>„Eli', Eli', lama' sabachtha'ni?“</i> det vil sige: „Min Gud, min Gud, hvorfor har du forladt mig?“<a onclick="return getMR(this,['ps 22:1','isa 53:10','mr 15:34']);" id="MR">+</a></vs>
 
Hi

Instead of handling the plus sign itself, I would prefer to exclude the element containing such navigational aid. In your sample code it has [tt]id[/tt] "MR", so I would do it like this :
JavaScript:
function getText(what)
[teal]{[/teal]
    [b]var[/b] result [teal]=[/teal] [green][i]''[/i][/green]

    [b]for[/b] [teal]([/teal][b]var[/b] i [teal]=[/teal] [purple]0[/purple][teal],[/teal] l [teal]=[/teal] what[teal].[/teal]childNodes[teal].[/teal]length[teal];[/teal] i [teal]<[/teal] l[teal];[/teal] i[teal]++)[/teal]
        [b]if[/b] [teal]([/teal]what[teal].[/teal]childNodes[teal][[/teal]i[teal]].[/teal]nodeType [teal]==[/teal] Node[teal].[/teal]TEXT_NODE[teal])[/teal]
            result [teal]+=[/teal] what[teal].[/teal]childNodes[teal][[/teal]i[teal]].[/teal]textContent
        [b]else[/b] [b]if[/b] [teal]([/teal]what[teal].[/teal]childNodes[teal][[/teal]i[teal]].[/teal]nodeType [teal]==[/teal] Node[teal].[/teal]ELEMENT_NODE [highlight][teal]&&[/teal] what[teal].[/teal]childNodes[teal][[/teal]i[teal]].[/teal]id [teal]!=[/teal] [green][i]'MR'[/i][/green][/highlight][teal])[/teal]
            result [teal]+=[/teal] [COLOR=darkgoldenrod]getText[/color][teal]([/teal]what[teal].[/teal]childNodes[teal][[/teal]i[teal]])[/teal]

    [b]return[/b] result[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/\s+/g[/fuchsia][teal],[/teal] [green][i]' '[/i][/green][teal])[/teal]
}
Note that [tt]id[/tt]s should be unique across the document. If there are multiple such elements, you should use [tt]class[/tt] instead.

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top