How to change text in a HTML element ?

255 views
Skip to first unread message

Nicolas Pinault

unread,
Jun 5, 2016, 3:32:42 PM6/5/16
to bry...@googlegroups.com
With the following code :

d = html.DIV('initial text')
d <= html.DIV()
d.text = 'new text'

I expect the text of d to be modified. That is the case. However, the
DIV inserted in d vanishes. Is this expected behaviour ?

Pierre Quentel

unread,
Jun 5, 2016, 4:50:58 PM6/5/16
to brython
Yes, it is the same as setting textContent in Javascript :

d = document.createElement('DIV')
d.appendChild(document.createTextNode('initial text'))

d1 = document.createElement('DIV')
d1.appendChild(document.createTextNode('additional text'))
d.appendChild(d1)

document.body.appendChild(d)

d.textContent = 'remplace'

This code replaces the content of d by a single text node, also removing the child d1

Glenn Linderman

unread,
Jun 5, 2016, 5:03:41 PM6/5/16
to bry...@googlegroups.com
.textContent and .innerHTML tend to wipe out everything, in Javascript. But there is also .nodeValue, in Javascript.

Of course, a DIV doesn't contain text: it contains elements, some of which may be text elements, and the .nodeValue of those text elements is the text within the DIV.

I assume Brython html implements something equivalent to .nodeValue, but I didn't look it up.

Nicolas Pinault

unread,
Jun 6, 2016, 3:01:01 PM6/6/16
to bry...@googlegroups.com

> Of course, a DIV doesn't contain text: it contains elements, some of
> which may be text elements, and the .nodeValue of those text elements
> is the text within the DIV.
To my knowledge <div>some text</div>, is correct.
So <div>some text <div>some other text</div></div>, is also correct.

Glenn Linderman

unread,
Jun 6, 2016, 3:47:23 PM6/6/16
to bry...@googlegroups.com
Limited knowledge, I guess.

The HTML syntax you cite is acceptable, but the DIV Element in the DOM does not contain text... it contains text nodes which contain text. See stackoverflow for more details (and to save me typing).

Maybe this can be clarified by saying that a <div> (HTML) can contain text, but if DIV (DOM) cannot.

Cheers!
 

Nicolas Pinault

unread,
Jun 6, 2016, 3:57:20 PM6/6/16
to bry...@googlegroups.com
Le 06/06/2016 à 21:47, Glenn Linderman a écrit :
On 6/6/2016 12:00 PM, Nicolas Pinault wrote:

Of course, a DIV doesn't contain text: it contains elements, some of which may be text elements, and the .nodeValue of those text elements is the text within the DIV.
To my knowledge <div>some text</div>, is correct.
So <div>some text <div>some other text</div></div>, is also correct.


Limited knowledge, I guess.
That's right.


The HTML syntax you cite is acceptable, but the DIV Element in the DOM does not contain text... it contains text nodes which contain text. See stackoverflow for more details (and to save me typing).
Thanks for the link.


Maybe this can be clarified by saying that a <div> (HTML) can contain text, but if DIV (DOM) cannot.
Well, I think you'll have to type again to explain this to many people. Like here and many other places.

 Cheers!
 
--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/e4bc9bce-4e7f-ceb2-f2c0-a4e7bb79e71d%40g.nevcal.com.
For more options, visit https://groups.google.com/d/optout.


Nicolas Pinault

unread,
Jun 6, 2016, 4:24:52 PM6/6/16
to bry...@googlegroups.com
Le 06/06/2016 à 21:56, Nicolas Pinault a écrit :
Le 06/06/2016 à 21:47, Glenn Linderman a écrit :
On 6/6/2016 12:00 PM, Nicolas Pinault wrote:

Of course, a DIV doesn't contain text: it contains elements, some of which may be text elements, and the .nodeValue of those text elements is the text within the DIV.
To my knowledge <div>some text</div>, is correct.
So <div>some text <div>some other text</div></div>, is also correct.


Limited knowledge, I guess.
That's right.

The HTML syntax you cite is acceptable, but the DIV Element in the DOM does not contain text... it contains text nodes which contain text. See stackoverflow for more details (and to save me typing).
Thanks for the link.

Maybe this can be clarified by saying that a <div> (HTML) can contain text, but if DIV (DOM) cannot.
Well, I think you'll have to type again to explain this to many people. Like here and many other places.
Glenn,
Re-reading my last sentence, I'm afraid, it can be misunderstood. Please, be assured, there is no offense.
I am learning by reading many posts, tutorials, examples,etc on the Internet.
Sometimes, it's hard to know if what is written on a page is right or not.
Often, it's hard to find the information I need.

Glenn Linderman

unread,
Jun 6, 2016, 11:37:55 PM6/6/16
to bry...@googlegroups.com
On 6/6/2016 1:24 PM, Nicolas Pinault wrote:
Le 06/06/2016 à 21:56, Nicolas Pinault a écrit :
Le 06/06/2016 à 21:47, Glenn Linderman a écrit :
On 6/6/2016 12:00 PM, Nicolas Pinault wrote:

Of course, a DIV doesn't contain text: it contains elements, some of which may be text elements, and the .nodeValue of those text elements is the text within the DIV.
To my knowledge <div>some text</div>, is correct.
So <div>some text <div>some other text</div></div>, is also correct.


Limited knowledge, I guess.
That's right.

The HTML syntax you cite is acceptable, but the DIV Element in the DOM does not contain text... it contains text nodes which contain text. See stackoverflow for more details (and to save me typing).
Thanks for the link.

Maybe this can be clarified by saying that a <div> (HTML) can contain text, but if DIV (DOM) cannot.

Not sure how that "if" got in there where it should say  "a DIV".


Well, I think you'll have to type again to explain this to many people. Like here and many other places.
Glenn,
Re-reading my last sentence, I'm afraid, it can be misunderstood. Please, be assured, there is no offense.
I am learning by reading many posts, tutorials, examples,etc on the Internet.
Sometimes, it's hard to know if what is written on a page is right or not.
Often, it's hard to find the information I need.

Before I read this second response, I was going to say that it is not my goal in life to correct everyone's misunderstandings. Not even if they make a posting or web page about it, asserting their misconception as true. But since you wrote your second response, I'll write more than that.

But I wouldn't have to type much, even in forums in which I participate a bit, because the stackoverflow link I cited explains it more thoroughly than I was going to... so it would just be a matter of pasting that, in places where needed.

One of the disadvantages of an open internet, is people can post utter rubbish, and some will believe it (spoof sites abound). Of course, the sites with accurate information are preferred, and not always easy to find, as you mention.  People get a misconception about something, and if they can empirically work from that misconception, they get self-assured of the correctness of that misconception... until they find the fallacy via something not working as they thought it would. The typical reaction in that case is that they think they found a bug in the code they are using, even though the reality is that the code they are using is correct, and their understanding of it or mental model of it was incorrect.  I've been there plenty of times myself...

Sometimes the real documentation for something can be obscure, and sites like stackoverflow and other tech help forums and blog posts have good explanations that are easier to understand, so using them can be helpful. But finding and reading the real documentation can help separate the truth from the rubbish, if the real documentation, even though perhaps hard to understand, seems to be saying something different than the forum or blog.

The DOM is documented and standardized by W3C, of course, and not only is it a large, complex document, there are no browsers that implement only the standardized parts... all of them have extensions, and the extensions are not all compatible, and the older browsers don't implement all the standardized parts either. So it is easy to get suckered into using this browser's extension or old API, and have your code fail on that browser. Even if you stick slavishly to the standard parts, you'll find bugs in this or that version of this or that browser, or old browsers that don't implement the standard you need, or even current browsers that haven't implemented it yet.

If you haven't yet found  caniuse.com you should. It doesn't have all the answers, but the ones it has seem to be pretty accurate.

Nicolas Pinault

unread,
Jun 7, 2016, 3:12:55 PM6/7/16
to bry...@googlegroups.com
This is exactly my point of view.
I'll just add this : Sometimes, the answers to the simplest questions are difficult to find.
For example : You find lot of descriptions of how to add formatted text (in html). Justified text, aligned text, bold text, coloured text... But what about "raw" text. How to add just simple plain text ? There's no <text> tag. You can't add it in a DIV (I know it now ;)) or other containers of this type. <p> implies some formatting. So how can I add text ? I suppose <span> is the answer. But, am I right ?

If you haven't yet found  caniuse.com you should. It doesn't have all the answers, but the ones it has seem to be pretty accurate.
Thanks for the link. Very useful.

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.

Glenn Linderman

unread,
Jun 7, 2016, 4:27:19 PM6/7/16
to bry...@googlegroups.com
Even <span> can have CSS referencing it to format it in whatever way the CSS specifies... either using ID or CLASS attributes, or element containment selectors that require no particular attributes.

From Javascript you can  document.createTextNode("my plain text") and then append or insert that node wherever you like (in another element that can contain nodes... a text node cannot contain other text nodes, just text). It will, of course, be affected by the default or specific CSS that applies to the parent element.


If you haven't yet found  caniuse.com you should. It doesn't have all the answers, but the ones it has seem to be pretty accurate.
Thanks for the link. Very useful.

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/4a05429a-1908-3aa6-24ef-dc96900365a4%40g.nevcal.com.
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.

Nicolas Pinault

unread,
Jun 7, 2016, 5:05:04 PM6/7/16
to bry...@googlegroups.com
Sure but it seems to be the most "generic" tag.


From Javascript you can  document.createTextNode("my plain text") and then append or insert that node wherever you like (in another element that can contain nodes... a text node cannot contain other text nodes, just text). It will, of course, be affected by the default or specific CSS that applies to the parent element.
From javascript. But from html ?


If you haven't yet found  caniuse.com you should. It doesn't have all the answers, but the ones it has seem to be pretty accurate.
Thanks for the link. Very useful.

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/4a05429a-1908-3aa6-24ef-dc96900365a4%40g.nevcal.com.
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/f8a25772-dba4-e5ae-004d-048144330bd7%40famillepinault.fr.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.

Billy Earney

unread,
Jun 7, 2016, 5:09:46 PM6/7/16
to bry...@googlegroups.com
I haven't followed all of this thread, so maybe I've missed something, but for raw text, couldn't you use the PRE tag?

Billy

Glenn Linderman

unread,
Jun 7, 2016, 5:42:36 PM6/7/16
to bry...@googlegroups.com
On 6/7/2016 2:04 PM, Nicolas Pinault wrote:
Even <span> can have CSS referencing it to format it in whatever way the CSS specifies... either using ID or CLASS attributes, or element containment selectors that require no particular attributes.
Sure but it seems to be the most "generic" tag.

Probably, as tags go.  It seems to have no pre-defined formats.



From Javascript you can  document.createTextNode("my plain text") and then append or insert that node wherever you like (in another element that can contain nodes... a text node cannot contain other text nodes, just text). It will, of course, be affected by the default or specific CSS that applies to the parent element.
From javascript. But from html ?

Just type the text, and it gets stuffed in one or more TextNode for you, as appropriate, depending on other markup.

Glenn Linderman

unread,
Jun 7, 2016, 5:48:31 PM6/7/16
to bry...@googlegroups.com
On 6/7/2016 2:09 PM, Billy Earney wrote:
I haven't followed all of this thread, so maybe I've missed something, but for raw text, couldn't you use the PRE tag?

<pre> results in the formatting of the ASCII control included in the text, if any. Some people think of that as plain text. I think plain text is just one character after another for a paragraph, with a double-CR (ASCII) or a double-<br> (HTML) between paragraphs. <pre> would set its width to the length of the longest (ASCII) paragraph, applying no line wrapping. Some people might think of that as plain text. Some people would put CR (ASCII) or <br> (HTML) every so often, and think of that as plain text.

Nicolas seems to equate "raw" and "plain" text. Some would perhaps differentiate the two types I describe above by using "raw" for one, and "plain" for the other. They are not well-defined terms with specific semantics.


Nicolas Pinault

unread,
Jun 8, 2016, 2:44:15 PM6/8/16
to bry...@googlegroups.com
Thanks for sharing your knowledge.

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages