/    Sign up×
Community /Pin to ProfileBookmark

Dynamically creating a DIV in IE

Hi everyone,

I am writing something in JS that involves dynamically creating DIVs. So this is basically how I do it:

Code:

var newDiv = document.body.appendChild (document.createElement (‘div’));
newDiv.setAttribute (‘id’, ‘someDiv’);
newDiv.setAttribute (‘className’, ‘someClass’);

Now the problem is when I set any of the event-firing attributes of this newly created DIV:

Code:

newDiv.setAttribute (‘onmouseover’, ‘alert (this.id)’);

which, when rolled-over, should bring up that good old message box saying ‘someDiv’. But it doesn’t, so I start thinking ok I’ve got something wrong somewhere so I do another of those good old things:

Code:

alert (document.body.innerHTML);

and reload to get the following message:

Code:

<DIV class=someClass id=someDiv onmouseover=”alert (this.id)”></DIV>

Does anyone have any idea of what is going on here? It’s totally OK if I just do a document.write (blah blah) to create the div, but I want this script to run on most browsers (argh, the web-designer’s dream!). Also, why does IE write the class=blah before the id=blah when I didn’t set those attributes in that order. More importantly, why doesn’t it include the quotes around the class=”blah” or id=”blah”? I tried to rectify this by doing:

Code:

newDiv.setAttribute (‘id’, ‘”someDiv”‘);
newDiv.setAttribute (‘className’, ‘”someClass”‘);

but still no use.

I know you are probably thinking I should be writing this message to Bill Gates, but I don’t think he uses JS anyway!?!

OK, thanks is advance, sorry for taking long (typical Australian way, gotta get everything sorted out in every teeny-weeny detail!)

Yanbu

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliJun 02.2003 — To attache an Event to an element in IE you must use

element.attacheEvent("onclick",finctionName);

and for NS

element.addEventListener( "click", functionName, false );


some differences you must be aware of..

in IE any event name string which you might pass to the function

element.attachEvent("onclick",finctionName);

where "onclick" is the event type string.

you have to make usre its prefixed with "on" where as in NSit must not be prefixed with "on"

Hope this helps

[b]EDIT:[/b]

Which spellings are you referring to Dave..?
×

Success!

Help @yanbuwebdesign 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...