/    Sign up×
Community /Pin to ProfileBookmark

Evaluating with INNERHTML

This is probably a very simple question to most of you but here goes:

I am trying to evaluate an expression. I have tons of <div id=””> in a template, named block##, where we loop through block 1, block 2, etc.

Each line item in the list (which contains its own div tag), can be called and I then want, in the JS function to alter the innerHTML state using the familiar id.innerHTML=’string’;

Problem is I find out I cannot use 4.innerHTML=”; it is invalid.

How do I, using a passed down integer, say 1, create using Javascript, the “phrase” block.innerHTML=”;

eval(“word”+id) doesn’t work. I get an “object” back when I use alert to see its value.

Thanks, Robert

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@FangJun 20.2004 — document.getElementById('block'+numID).innerHTML="";
Copy linkTweet thisAlerts:
@David_HarrisonJun 20.2004 — It sounds like you have a list div tags and they are in order. The correct way to mark that up would be as an ordered list. Here is what I suggest changing your markup to:&lt;ol id="parent_id"&gt;
&lt;li&gt;This is block 1&lt;/li&gt;
&lt;li&gt;This is block 2&lt;/li&gt;
&lt;li&gt;This is block 3&lt;/li&gt;
&lt;li&gt;This is block 4&lt;/li&gt;
&lt;/ol&gt;
This is more semantic and the <ol> can act as the parent tag which is then used to reference the <li>'s.

If in your current code all of the div tags are in the same parent element, you could reference them like this:document.getELementBtId("parent_id").getElementsByTagName("div")[numID].innerHTMLOf course innerHTML is invalid so you shouldn'y be using it, but since I don't know what in particular you are accessing I can't recommend anything else to use though.
Copy linkTweet thisAlerts:
@britinusaauthorJun 21.2004 — Fang

Your solution did the trick. Thanks!

Lavalamp, I don't understand why you say innerHTML is invalid and that I shouldn't be using it?

What I am curious about is how to reference these <div> blocks, loop them through and blank them all out. For example:

In Cold Fusion I have a list variable 1,2,3,4,5 in the var #shown#

in the js function,

function edit(id){

<cfloop list="#shown#" index="t">

r#t#.innerHTML='';

</cfloop>

}

How should the second line's syntax be structured?

Robert
Copy linkTweet thisAlerts:
@David_HarrisonJun 21.2004 — innerHTML does not appear in the W3C sepcification and guidelines for the DOM, I think that it was originally an IE only thing but because so many webdevelopers used it, support for it was added to other browsers. Now that IE is dying out, it might not be supported in future browsers.function edit(id){
&lt;cfloop list="#shown#" index="t"&gt;
while(document.getElementById("r#t#").childNodes.length&gt;0){
document.getElementById("r#t#").removeChild(document.getElementById("r#t#").firstChild)='';
}
&lt;/cfloop&gt;
}
Copy linkTweet thisAlerts:
@britinusaauthorJun 21.2004 — I just inserted it exactly as shown:

<cfloop list="#msgs_shown#" index="t">

while(document.getElementById("r#t#").childNodes.length>0){

document.getElementById("r#t#").removeChild(document.getElementById("r#t#").firstChild)='';

}

</cfloop>

and got an Object required error.

Could you forward it with the innerHTML syntax. I have to do FAR more js reading .. thanks!
Copy linkTweet thisAlerts:
@David_HarrisonJun 21.2004 — Do you have a link to this code, or can you perhaps upload the script if it's not online?
×

Success!

Help @britinusa spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...