Dev.Opera - Follow the standards, break the rulesDev.Opera - Follow the standards, break the rules

Login

Lost password?

Forums » Article Discussions

Discuss the articles posted on Dev.Opera.

Note: You need to login to post in the forums. if you don't have an account you first need to sign up.

By stuartlangridge anchor Tuesday, 3. February 2009, 06:38:23

47: Creating and modifying HTML

When you have learned how to select specific HTML elements in the DOM using JavaScript, the next stage is to start modifying existing elements and adding new ones in response to user actions. In this article, Stuart Langridge show you how to do this by way of some classic examples, such as tabbed interfaces.

( Read the article )

By Andrew Gregory anchor Thursday, 5. February 2009, 07:32:32

avatarLike an earlier article, there is a style issue with declaring variables. Javascript variables have function scope, and so having vars deep inside a function is misleading, especially for programmers who are used to languages where variables have block scope. Javascript functions should declare all their variables once at the beginning of the function.

By jozefkrivan anchor Wednesday, 1. April 2009, 12:40:36

avatarHi to all. I am confused by part of the article:
"Adding the element to the document can be done in one of two ways: using appendChild as above,
or using insertBefore. The appendChild function adds our new element at the end of an existing
element (that’s why it’s called append). Since we want to add it in the middle of an existing
element, we need insertBefore."
I have understood, I need appendChild function to get text into newly created span element and of course I need to insert this whole element to document, then needed to use insertBefore. My confusion arrise from "Adding the element to the document can be done in one of two ways:..". My understandig is, I need both function to make it working. And then my confusion arrise more in "Excercise questions" by point "What’s the difference between appendChild and insertBefore?" I understood this two functions making diferent things, first append text to span element, second insert whole element to document.
Should someone make me clear where coming my confusion from? Thank you in advance, Jozef

By codebyjoe anchor Wednesday, 1. April 2009, 16:22:21

avatarboth functions are for placing a child node into its new parent.

a child node may be
- an element node like an ordered list or a paragraph
- a text node
- an attribute
- a comment

see
http://www.devguru.com/technologies/xml_dom/16049.asp (append child)
http://www.devguru.com/technologies/xml_dom/16052.asp (insertBefore)

By Coyotee anchor Saturday, 13. June 2009, 16:46:25

avatarTo - jozefkrivan :smile:
I would like to give your question a go. :no:
To recap:

A. All text in an HTML document is contained within textNodes.
Create textNodes separately from the P/H1/SPAN elements.
Meld textNodes and P/H1/SPAN elements together by using appendChild().

B. Then add the span element into the document.
This could be nowhere special or as in the lesson,
Within a link node, before next portion of parent text node using insertBefore().

Copy the following markup into an html file and see how it works. Remove the comments on 4a and
apply comments on 4b to see the difference using an Opera browser.

They differ as to how to modify the DOM around textNodes.

You will have to fix the close link ( < / a > ) after the word Opera in the link below. Good Luck!

<html><style>span{color:red}</style><body>
<p>Now is the time for <a id='A' href='www.opera.com'>Opera< / a > Order.</p>
</body><script>
// 1. create a <span> element using "document.createElement('span')"
var spanelem = document.createElement('span');

// 2. create a "textNode" using "document.createTextNode(' New World ')"
var spantext = document.createTextNode(' New World ');

// 3. put the textNode 'inside' the span using appendChild()
spanelem.appendChild(spantext);

// The next step would be to place the text in the DOM as you could do here.
// 4a. add span element into document - no special place
//document.documentElement.appendChild(spanelem);

//But the lesson places it somewhat more specifically.
//It inserts the span element "before the thing after a link."
//<p> Now is the time for <a id='A' href='www.opera.com'> Opera < / a > [HERE] Order. </p>

// 4b. add span element into document - "within" a link node before next portion of parent text node
lnk=document.getElementById('A');
lnk.parentNode.insertBefore(spanelem, lnk.nextSibling);

</script></html>

Post edited Saturday, 13. June 2009, 21:23:18

By Volvoxpl anchor Thursday, 25. June 2009, 14:51:32

avatarIm begginer in JS, but i dosnt really understand this part:

var el = document.getElementById('mypara');
mypara.style.display = 'none';


Why there is a
mypara.style.display
, and not
el.style.display
?
Well it seems, both versions work, but with your version there is no need of creating variable el

Moderators: pepelsbey | dstorey | mcx | operadev | chrismills | shwetankdixit | brucelawson | iheni | andreasbovens | zibin | mollydotcom