/    Sign up×
Community /Pin to ProfileBookmark

creating html within javascript

Hi All,

I have been trying to use innerHTML to construct a very simple two layered menu system.

On mousover on an image in the upper line, the html calls a function which swaps the button image, and then replaces the whole set of images underneath that line with the child set belonging to that button. This works fine when I’m just substituting text, but I want to substitute a constructed line of html instead of text.

I have tried using eval, but it doesn’t seem to work.

This works;

function swap(imgName, imgRef, menRef)

{

test.innerHTML = “test”;

imgOn = eval(imgName + ‘b.src’);
document[imgName].src = imgOn;

swapPic = eval(imgRef + ‘.src’);
document.images[‘submenu’].src = swapPic;

}

This doesn’t work AND the swap parts of the function cease working too;

function swap(imgName, imgRef, menRef)

{

test.innerHTML = eval(“<p style=”+/”+”margin-top: 0; margin-bottom: 0″+/”+”><img src=”+/”+”images/menu/blank.jpg”+/”+” border=”+/”+”0″+/”+” width=”+/”+”802″+/”+” height=”+/”+”20″+/”+”>”);

imgOn = eval(imgName + ‘b.src’);
document[imgName].src = imgOn;

swapPic = eval(imgRef + ‘.src’);
document.images[‘submenu’].src = swapPic;

}

What have I done wrong?

Why do the document functions cease working when the innerHTML function fails? (Note that it doesn’t matter what order I put them in.)

Is javascript really a fickle as I am starting to suspect?

ctb

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@ibidris2003Aug 04.2003 — Hi,

it looks like you are trying to escape quotes with / instead of . This might be causing the errors

http://galileo.spaceports.com/~ibidris/
Copy linkTweet thisAlerts:
@chestertbauthorAug 07.2003 — Doh!

Big oops.

However!!!

I fixed that, and it still does not work.

if I use

innerHTML="test";

it places

test

in the appropriate place.

if I use

innterHTML=test.innerHTML = eval("<img border="+"+"0"+"+" src="+"+ "images/picturestrip.jpg"+"+">");

generates an error "Invalid Character"

if I try something really simple, like

innerHTML=eval("<b>test</b>")

it generates a syntax error

note the actual code is as follows;

function highlight(imgName, imgRef, menRef)

{

switch (menRef)

{

case 'menu0' :

submenu.innerHTML = "this really sucks"

break;

case 'menu1' :

submenu.innerHTML = eval("<b>test<b>");

break;


case 'menu2' :

submenu.innerHTML = "menu2";

break;

case 'menu3' :

submenu.innerHTML = "menu3";

break;


case 'menu4' :

submenu.innerHTML = "menu4";

break;


case 'menu5' :

submenu.innerHTML = "menu5";

break;

}


imgOn = eval(imgName + 'b.src');
document[imgName].src = imgOn;



}

If the mouse rolls over anything but option 1, it's fine. Option one generates the error.

CTB
Copy linkTweet thisAlerts:
@CharlesAug 07.2003 — [font=georgia]1) You don't need to wrap your strings in "eval" functions. It only slows down your processing.

2) Both HTML and JavScript recoginze both single and double quotes. I would suggest that you restrict yourself to using single quotes for the JavaScript and double quotes for the HTML.[/font]
Copy linkTweet thisAlerts:
@ibidris2003Aug 07.2003 — submenu.innerHTML = eval("<b>test<b>");

is suspicious. Should be submenu.innerHTML = "<b>test<b>";

http://galileo.spaceports.com/~ibidris/
Copy linkTweet thisAlerts:
@Mr_JAug 07.2003 — Post your full code..

You do not eval this

test.innerHTML = eval("<p style="+/"+"margin-top: 0; margin-bottom: 0"+/"+"><img src="+/"+"images/menu/blank.jpg"+/"+" border="+/"+"0"+/"+" width="+/"+"802"+/"+" height="+/"+"20"+/"+">");


it should be

test.innerHTML = "<p style="margin-top: 0; margin-bottom: 0"><img src="images/menu/blank.jpg" border="0" width="802" height="20">"
Copy linkTweet thisAlerts:
@chestertbauthorAug 07.2003 — Magic!

Absolutely... thank you!

Seems so simple now that I've seen it written.

Works a treat.

CTB
Copy linkTweet thisAlerts:
@Mr_JAug 07.2003 — Yeh .. . . ... . you have to be careful when using the eval() method

?
×

Success!

Help @chestertb 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.29,
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,
)...