Connecting Tech Pros Worldwide Help | Site Map

How do I change HTML objects with my JS strings ?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 12:01 PM
H
Guest
 
Posts: n/a
Default How do I change HTML objects with my JS strings ?

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





  #2  
Old July 20th, 2005, 12:01 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default Re: How do I change HTML objects with my JS strings ?

"H" <nospamplease__henke0001@hotmail.com> writes:
[color=blue]
> 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;[/color]

You want 'v = "place" +i;'. What you have here will give you "place1",
"place12", "place123", etc.
[color=blue]
> // Here I want to access the resource in the HTML doc, whos id="place1",
> id="place2", etc etc, so how can I do that ?[/color]

var elem = document.getElementById(v);
[color=blue]
> // like "theValueInv".innerText = "bla bla bla bla"; if the resource
> with id="place1" is for ewxample a <p>[/color]

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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
  #3  
Old July 20th, 2005, 12:01 PM
Richard Cornford
Guest
 
Posts: n/a
Default Re: How do I change HTML objects with my JS strings ?

"H" <nospamplease__henke0001@hotmail.com> wrote in message
news:i8fvb.38604$dP1.142394@newsc.telia.net...[color=blue]
> 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>
> }[/color]

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.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.