By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,668 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

How do I change HTML objects with my JS strings ?

P: n/a
H
Ok, heres the deal ;

I have my script code like this

var v = "place";
for (var i = 0 ; i < 5 ; i++)
{
v = v + i; // or v = "place" +i;
// Here I want to access the resource in the HTML doc, whos id="place1",
id="place2", etc etc, so how can I do that ?
// like "theValueInv".innerText = "bla bla bla bla"; if the resource
with id="place1" is for ewxample a <p>
}
I hope you clever guys can help this newbie with this ...
TIA
H


Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"H" <no*********************@hotmail.com> writes:
Ok, heres the deal ;

I have my script code like this

var v = "place";
for (var i = 0 ; i < 5 ; i++)
{
v = v + i; // or v = "place" +i;
You want 'v = "place" +i;'. What you have here will give you "place1",
"place12", "place123", etc.
// Here I want to access the resource in the HTML doc, whos id="place1",
id="place2", etc etc, so how can I do that ?
var elem = document.getElementById(v);
// like "theValueInv".innerText = "bla bla bla bla"; if the resource
with id="place1" is for ewxample a <p>


You can use innerText, but that only works in IE and Opera 7. Or you
could use DOM methods that works in all modern browsers (for IE, here
that means IE 5+)

// clear old content
while(elem.hasChildNodes()) {
elem.removeChild(elem.lastChild);
}
// add new
elem.appendChild(document.createTextNode("bla bla bla bla"));

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
"H" <no*********************@hotmail.com> wrote in message
news:i8********************@newsc.telia.net...
var v = "place";
for (var i = 0 ; i < 5 ; i++)
{
v = v + i; // or v = "place" +i;
// Here I want to access the resource in the HTML doc, whos
//id="place1", id="place2", etc etc, so how can I do that ?
// like "theValueInv".innerText = "bla bla bla bla"; if the
//resource with id="place1" is for ewxample a <p>
}


Look at:-

<URL: http://www.jibbering.com/faq/#FAQ4_39 >

-and the linked resource.

But if your interest is on locating elements within a document that have
a specific (and unique, as required) ID then the document.getElementById
function is probably the best option:-

function getEl(id){
if(document.getElementById){ //all DOM browsers
return document.getElementById(id);
}else if(document.all){ //IE 4 fall-back
return document.all[id];
}else if(document.layers){ //Netscape 4
return document.layers[id];//but that will only find positioned
//elements on Netscape 4.
}
return null; //no suitable method exists on this browser.
}

....
var el, v = "place";
for(var c = 0;c < 5;c++){
el = getEl(v+c);
if(el){ //so only if the returned value was non-null.
... //do something with the element.
}
}
....

Richard.
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.