getElementById replaces text, i want something that doesn't

Joined
12 Feb 2006
Posts
17,639
Location
Surrey
ok another small problem, i have a for loop and every loop it displays a link by getElementId and innerHTML.

the trouble with this rather then adding a new link
added to the element, it replaces everyone there.

how can i get it to not replace but add to the element?

thanks
 
Code:
document.getElementById('id').innerHTML += 'whatever you want';

that what you're after?

if you want to add another link to the list, check out clondeNode and appendChild
 
Seriously, I'd check out DOM manipulation rather than .innerHTML... has serious benefits.

ok will do, is innerHTML javascript and DOM some sort of html? thats the what i was lead to beleive and this work i am doing is for coursework to show off javascript which is why i hadn't looked into DOM just yet.

while i have this thread open i got a small new problem. now i got the links to the sets to display fine im trying to get them to do what they are there for, to change set according to which number they are. now the problem i am having is sending the value of the link which is created by a loop through a function, the number for each loop isn't different, it is only the last loop amount, so in my case instead i got set 1, set 2, set 3, set 4, when the user clicks set 2 the value being 2 should be sent, but its always 4. is there a way to get value to be the same as the set?

part of my code

Code:
for  (y=1;y<=pageAmount;y++)

{ <p onclick='jumpSet(y)'>set " + y + "</p> "; }

it shows as a link for set 1, set 2, but the y value in jumpSet(y) is always the last loops y value
 
Seriously, I'd check out DOM manipulation rather than .innerHTML... has serious benefits.

what DOM function would you use to access the text between tags, as opposed to innerHTML? I'm interested, as I always use innerHTML for this, but if there's something better I can use, then that's great.

Mammalian, DOM (as I understand it) is an objective representation of html elements and attributes. For example, getElementById is a DOM function used to search Document Objects by their id attribute - I always thought that innerHTML was a property of an HTML object that gives you access to the text between opening and closing HTML tags.
 
Last edited:
Yeah I'd agree DOM is nicer.

You can just do something like.

create UL node.

then in a loop
{
LINode = add <Li> node
Anode = liNode.addChild(add <a> node)
linkTextNode = Anode.addChildText(add text)
}

obv that is not correct syntax at all, but you get the idea.
 
what DOM function would you use to access the text between tags, as opposed to innerHTML? I'm interested, as I always use innerHTML for this, but if there's something better I can use, then that's great.

Mammalian, DOM (as I understand it) is an objective representation of html elements and attributes. For example, getElementById is a DOM function used to search Document Objects by their id attribute - I always thought that innerHTML was a property of an HTML object that gives you access to the text between opening and closing HTML tags.

As I understand it, .childNodes lists all of the nodes of the current node, including text nodes, etc etc. You can use .appendChild() to add a new child node to the DOM rather than call .innerHTML which needs to be reinterpreted (and incidently, will be phased out by a later version of the standard due to it not being applied to XML).

I have used .childNodes and .appendChild() but not to add children to a specific point in the childNodes array... I'd suggest you get Firefox + Firebug and have a browse in your DOM and see what it is that is being generated, and what it is you actually want.
 
yeah, but those are 2 different things you're talking about there. if I have an unordered list, and I want to change/add to the text in one of the list items, appendChild isn't going to help. If I want to add a new list item, then obviously I'd use that, but for changing the contents I thought innerHTML was the only way.

In my original post, I said I was unclear as to what the OP wanted to do - if he wanted to add to the text using the concatenation operator, or whether he wanted to create a new node.

Sorry if I've caused any confusion - it would seem that I have! :o

and for the record - changing the contents of innerHTML is DOM manipulation
 
Back
Top Bottom