473,789 Members | 2,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

InnerHTML not grabbing entire HTML if <p> is present

Let me preface this with the fact that I am a newbie to HTML, XML and
Javascript. Having said that, let me explain my dilemma:

I am having a difficult time getting innerHTML to consistently return
the entire HTML string when a <p> is part of the text. It somehow
throws everything off. I am trying to store exam questions in an
Oracle Database as XML.
The XML I started with looked like this:

<aicpcu id="XMLTEXT"><s tem id="STEM">This is my stem<p>this is the
second line</p></stem><options id="OPTIONS"><d istracter
id="DISTRACTER" >distracter 1</distracter>"><d istracter
id="DISTRACTER" >distracter 2</distracter>"><d istracter
id="DISTRACTER" >distracter 3</distracter>"><d istracter
id="DISTRACTER" >distracter 4</distracter></options></aicpcu>

I used this code to get at the text I wanted for the stem:
var s = document.getEle mentById("STEM" );
alert(s.innerHT ML);

I would only get This is my stem.

However, when I changed the XML to:
<div id="STEM" mix="Y">This is my stem<p><b>This is the second
line.</b></p></div>

I would get both lines.

Now for the distracter part. Since I got the stem working properly, I
tried to modify what I had in a similar manner. I've tried many
iterations of this, but this gives me the closest to what I want;
<div id="DISTRACTERS "><distract er id="DISTRACTER " label="A">First
distracter</distracter><dis tracter id="DISTRACTER " label="B"><b>Se cond
distracter</b></distracter><dis tracter id="DISTRACTER "
label="C"><i>Th ird distracter</i></distracter><dis tracter
id="DISTRACTER " label="D">Fourt h distracter<p>se cond
sentence</p></distracter></div>

The code I use to extract the innerHTML is:
var d = document.getEle mentById("DISTR ACTERS");
alert(d.childNo des.length); -- returns 5 (the <p> apparently shows as a
separate child)
var dList = document.getEle mentsByTagName( "DISTRACTER ");
alert(dList.len gth); -- returns 4 (correct)
for (var j=0; dList.length > j; j++)
{
htp.p(' alert(dList[j].innerHTML);');
}

All the innerHTML is displayed properly except the 4th one that has 2
paragraphs to it. I just get the text "Fourth distracter".

Any help would be greatly appreciated.

Mar 28 '06 #1
7 1757


MaryA wrote:

The XML I started with looked like this:

<aicpcu id="XMLTEXT"><s tem id="STEM">This is my stem<p>this is the
second line</p></stem><options id="OPTIONS"><d istracter
id="DISTRACTER" >distracter 1</distracter>"><d istracter
id="DISTRACTER" >distracter 2</distracter>"><d istracter
id="DISTRACTER" >distracter 3</distracter>"><d istracter
id="DISTRACTER" >distracter 4</distracter></options></aicpcu>

I used this code to get at the text I wanted for the stem:
var s = document.getEle mentById("STEM" );
alert(s.innerHT ML);


Why do you expect innerHTML to make any sense on XML? What kind of
document is that document object you call getElementById on?
--

Martin Honnen
http://JavaScript.FAQTs.com/
Mar 28 '06 #2
It is just sitting in an HTML document. It is written at the bottom of
document, for now. I have to find a way to hid it and still be able to
access it. If I put it in as a comment, the getElementById does not
find it. I was under the impression that the DOM standard can be used
to access XML and HTML. It seems to work, in part.

Mar 28 '06 #3
"MaryA" <ar******@cpcui ia.org> writes:
I am having a difficult time getting innerHTML to consistently return
the entire HTML string when a <p> is part of the text. It somehow
throws everything off. I am trying to store exam questions in an
Oracle Database as XML.
The XML I started with looked like this:

<aicpcu id="XMLTEXT"><s tem id="STEM">This is my stem<p>this is the
second line</p></stem><options id="OPTIONS"><d istracter
id="DISTRACTER" >distracter 1</distracter>"><d istracter
id="DISTRACTER" >distracter 2</distracter>"><d istracter
id="DISTRACTER" >distracter 3</distracter>"><d istracter
id="DISTRACTER" >distracter 4</distracter></options></aicpcu>
Your first problem is that your are using the HTML DOM, and probably
an HTML parser, on something that isn't HTML. It's just XML.

I used this code to get at the text I wanted for the stem:
var s = document.getEle mentById("STEM" );
alert(s.innerHT ML);

I would only get This is my stem.
My guess is that the parser, not knowing the "stem" element, is
reading "This is my stem" as part of an inline element (or should it
be "span"?). That element is ended by the opening tag of the block
level element "p".
However, when I changed the XML to:
<div id="STEM" mix="Y">This is my stem<p><b>This is the second
line.</b></p></div>

I would get both lines.
Because "div" is a block level element that can contain other
block level elements, so the "p" element can be part of it.
The code I use to extract the innerHTML is:
var d = document.getEle mentById("DISTR ACTERS");
alert(d.childNo des.length); -- returns 5 (the <p> apparently shows as a
separate child)


Again, the same explanation can hold. If the "distratcte r" element
is considered an inline element, then the <p> ends it and starts
a new child.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Mar 28 '06 #4
"MaryA" <ar******@cpcui ia.org> writes:
It is just sitting in an HTML document.
Well it's not an HTML document then, since it doesn't follow the
DTD of any version of HTML. The parser appears to parse it as
"tag soup", with gratuitous amounts of error correction. It's
anybody's guess how each browser will treat non-standard codes.
It is written at the bottom of document, for now. I have to find a
way to hid it and still be able to access it.
IE supports "XML islands" inside (otherwise) HTML documents. Other
browsers doesn't. Why use XML at all? If you want to access it with
Javascript anyway, you could just create a Javascript representation
of the data directly (e.g., using a JSON library on the server).
If I put it in as a comment, the getElementById does not find it.
Obviously, since it's not elements in the DOM any more.
I was under the impression that the DOM standard can be used to
access XML and HTML.
The DOM standard doesn't care what the element names of the document
are. The parser that processes the character sequence and turns it
into DOM elements do, since it is expecting HTML. HTML have specific
rules about which elements can be children of which other
elements. E.g., a div element cannot be the child of a paragraph (p)
element. Parsers attempts to be nice to you by accepting any tag
soup, and then do error correction on the document to try to make
it into a valid HTML document. This includes ending elements where
necessary, so the invalid HTML:
<p>lala<div>did i</div>lala</p>
is read as:
<p>lala</p><div>didi</div>lala
where all tags occour in an allowed order.

You are using non-HTML tags. The parser is, understandably, confuzed.
It appears the browser you use guesses unknown elements to be inline
elements, which cannot contain the block level p element.
It seems to work, in part.


That's also known as "not working" :)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Mar 28 '06 #5
MaryA said on 29/03/2006 3:57 AM AEST:
It is just sitting in an HTML document. It is written at the bottom of
document, for now. I have to find a way to hid it and still be able to
access it. If I put it in as a comment, the getElementById does not
find it. I was under the impression that the DOM standard can be used
to access XML and HTML. It seems to work, in part.


Just to be clear on what is happening, the browser parses the 'HTML' and
turns it into a DOM. The innerHTML property is a serialisation of the
DOM back to HTML.

innerHTML is not the same as 'view source'. For example, given the
valid HTML source:

<div>foo<p> bar</div>
In Firefox, the innerHTML property of the div is shown as:

foo<p> bar</p>

and in IE as:

foo
<P>bar</P>
Both browsers close the P tags, IE also capitalises the tag names,
inserts a return before the <P> tag and removes the leading whitespace
before 'bar'. So even in this apparently trivial case there are
significant differences between the source and innerHTML property, and
between browsers.

I guess you could say IE is 'right' because innerHTML is a proprietary
Microsoft invention that has been widely copied. It is not defined in
any standard and behaves differently in various browsers. Depending on
it to be supported consistently is not a good idea, though it is very
handy in some situations.
--
Rob
Mar 29 '06 #6
Ok - given what I've read here - I did some more investigating and came
up with this code:
function loadForm()
{
var theString =''<?xml version=\"1.0\" ?><aicpcu
id="XMLTEXT"><s tem id="STEM" mix="N">This is my stem<p>with 2 lines in
it!</p></stem><options id="OPTIONS" cnt="3"><distra cter id="DISTRACTER "
label="A" mix="N">distrac ter text 1</distracter><dis tracter
id="DISTRACTER " label="B" mix="N">distrac ter text
2</distracter><dis tracter id="DISTRACTER " label="C" mix="N">distrac ter
text 3</distracter></options></aicpcu>'';
var parser = new DOMParser();
var dom = parser.parseFro mString(theStri ng, "text/xml");
var root = dom.getElements ByTagName("aicp cu")[0];
var stem = root.getElement sByTagName("ste m");
for (var i = 0 ; i < stem.length ; i++)
{
var stemEl = stem[i];
var stemText = stemEl.firstChi ld.nodeValue;
alert(stemText) ;
}
alert("done");
}

But I still only get the first part of the stem (This is my stem) and
not the "with 2 lines in it" part. What the heck am I doing wrong now?

Mar 30 '06 #7
MaryA wrote:
Ok - given what I've read here - I did some more investigating and came
up with this code:
function loadForm()
{
var theString =''<?xml version=\"1.0\" ?><aicpcu ^^ id="XMLTEXT"><s tem id="STEM" mix="N">This is my stem<p>with 2 lines in
it!</p></stem><options id="OPTIONS" cnt="3"><distra cter id="DISTRACTER "
label="A" mix="N">distrac ter text 1</distracter><dis tracter
id="DISTRACTER " label="B" mix="N">distrac ter text
2</distracter><dis tracter id="DISTRACTER " label="C" mix="N">distrac ter
text 3</distracter></options></aicpcu>''; ^^ var parser = new DOMParser(); if (parser)
{ var dom = parser.parseFro mString(theStri ng, "text/xml"); if (dom)
{ var root = dom.getElements ByTagName("aicp cu")[0]; if (root)
{ var stem = root.getElement sByTagName("ste m");
for (var i = 0 ; i < stem.length ; i++)
for (var i = 0, len = stem.length; i < len; i++)
{
var stemEl = stem[i];
var stemText = stemEl.firstChi ld.nodeValue;
alert(stemText) ;
}
alert("done"); }
}
} }

But I still only get the first part of the stem (This is my stem) and
not the "with 2 lines in it" part. What the heck am I doing wrong now?


It is rather astonishing that you get anything at all.

| Error: syntax error
| Source file: javascript:var theString =''<?xml
| version=\"1.0\" ?><aicpcuid="XM LTEXT"><stem id="STEM" mix="N">This is my
| stem<p>with 2 lines init!</p></stem><options id="OPTIONS"
| cnt="3"><distra cter id="DISTRACTER" label="A" mix="N">distrac ter text
| 1</distracter><dis tracterid="DIST RACTER" label="B" mix="N">distrac ter
| text2</distracter><dis tracter id="DISTRACTER " label="C"
| mix="N">distrac tertext 3</distracter></options></aicpcu>'';
| Line: 1, Column: 18
| Source code:
| var theString =''<?xml version=\"1.0\" ?><aicpcuid="XM LTEXT"><stem
--------------------^
| id="STEM" mix="N">This is my stem<p>with 2 lines init!</p></stem><options
| id="OPTIONS" cnt="3"><distra cter id="DISTRACTER" label="A"
| mix="N">distrac ter text 1</distracter><dis tracterid="DIST

Either you have been using the ' character too often, or you should not
use ' and parseFromString () at all, because the E4X InputElementReg Exp
literal expression already creates an object.
PointedEars
Mar 30 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1550
by: Steven T. Hatton | last post by:
I believe the answer is that the language doesn't support the ability to retain a reference to a std::indirect_array<>, and would be illadvised to try, but I figure I'll ask just to be sure. What I mean by retaining a handle is that I would like to have a class that has a member field functioning as a reference into a valarray initialized by an indirect_array. template<typename T> class IA{ indirect_array<T>& ia_;
1
1226
by: iam247 | last post by:
Hi I am fairly inexperienced with ASP, SQL and VB Script. In one of my web pages I am trying to search a table in access which has a number of integers (GroupID's) (1 to 5 at present) and find those which do not match another set of GroupID's already contained in a record set named rsGroupsJ (1 to 4 at present). I want to find the GroupID's which do not match ie GroupID
3
1651
by: GeRmIc | last post by:
Hello Friends, I am working on a project where the service provider is written in JAVA and the client is written in .NET (C#). The Server communicates with the client through Java Beans. (1) I get an error in my .NET code when I instantiate the particular web service, the error says that a org.sax.exception : deserializer for QueryBean qb is
2
15953
by: ESPNSTI | last post by:
Hi, I'm trying to use a generics dictionary with a key class that implements and needs IComparable<>. However when I attempt to use the dictionary, it doesn't appear to use the IComparable<> to find the key. In the example below, accessing the dictionary by using the exact key object that was used to add to the dictionary works. (see code comment 1). However, if I attempt to access the dictionary by using a key object that
4
1567
by: Neil Zanella | last post by:
Hello, I would like to know what the difference is among the constructs <%= %> for evaluating an expression and displaying the evaluated result on the page and <%# %>. In particular I would like to know why the <%# %> construct is necessary in locations where the other <%= %> does not do. It seems to me that there is no equivalent of the <%# %> construct in PHP, thus I thought I would post to clarify. Is this an ASP.NET specific...
5
1896
by: ad | last post by:
I find there are some tag like <%= %> and <%# > in my .aspx file. What is the diffreence between <%= %> and <%# %>
6
6006
by: nicolas.rolland | last post by:
Would anyone know the reson why IList<Tdoes not implements IList ?? This results in strange behaviours, like typeof(IList).IsAssignableFrom(typeof(List<string>)) --true typeof(IList).IsAssignableFrom(typeof(IList<string>)) --false
1
2245
by: yawnmoth | last post by:
I'm trying to mess around with PHP5's DOM functions and have run into something that confuses me: <?php $dom = new DOMDocument(); $dom->loadHTML('<html></html>'); echo $dom->childNodes->length; ?>
17
4851
by: roN | last post by:
Hi, I'm creating a Website with divs and i do have some troubles, to make it looking the same way in Firefox and IE (tested with IE7). I checked it with the e3c validator and it says: " This Page Is Valid XHTML 1.0 Transitional!" but it still wouldn't look the same. It is on http://www.dvdnowkiosks.com/new/theproduct.php scroll down and recognize the black bottom bar when you go ewith firefox(2.0) which isn't there with IE7. Why does...
1
2253
by: John | last post by:
Hi var poster="<html><head..... etc .... </html>"; var animal='dog'; The string contains images and text that changes. Originally I wanted to do something like print "<a href=" + poster + ">Choose Poster of a " + animal + "</a>";
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10142
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3703
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.