Connecting Tech Pros Worldwide Forums | Help | Site Map

JavaScript/DOM: Can't set onclick-attribute for <a>'-tags in IE

Reinhold Schrecker
Guest
 
Posts: n/a
#1: May 22 '06
Hi there,

I am trying to generate a dynamic menu with JavaScript/DOM and have
problems to set the onclick-attribute for my <a>-elements.

The following code works fine in Opera and Mozilla but does not work in
IE:

<!--snip-->
<script language="JavaScript">
<!-- //

var topArray = new Array();

var j = topArray.length;
topArray[j] = new Object();
topArray[j]["id"] = "funktion_466";
topArray[j]["class"] = "top-function";
topArray[j]["a_id"] = "466";
topArray[j]["a_class"] = "menu-left-tab";
topArray[j]["a_onclick"] = "
top.open('wrap_action_frame?setup_dynamic_frame%3F par_session=1676053637&par_session=1676053637',
'dynamic1676053637'); return false;";
topArray[j]["a_text"] = "Search";


var myCurrentElement;
var myNewElement;
var myNewChildElement;
var myNewChildText;




function buildMenu(parameter)
{
myCurrentElement = window.document.getElementById('topmenu');
for (var i = 0; i < topArray.length; i++)
{
myNewElement = window.document.createElement('div');
myNewElement.className = topArray[i]["class"];
myNewElement.setAttribute('id', topArray[i]["id"]);

myNewChildElement = window.document.createElement('a');
myNewChildElement.setAttribute('id', topArray[i]["a_id"]);
myNewChildElement.className = topArray[i]["a_class"];


myNewChildElement.setAttribute('href', topArray[i]["href"]);
myNewChildElement.setAttribute('target', topArray[i]["target"]);

myNewChildElement.setAttribute(''onClick'',
topArray[i]["a_onclick"]);
}
}
buildMenu();
// -->
</script>
<!--snip-->

Regards
Reinhold


Martin Honnen
Guest
 
Posts: n/a
#2: May 22 '06

re: JavaScript/DOM: Can't set onclick-attribute for <a>'-tags in IE




Reinhold Schrecker wrote:

[color=blue]
> I am trying to generate a dynamic menu with JavaScript/DOM and have
> problems to set the onclick-attribute for my <a>-elements.[/color]

With the HTML DOM don't use the setAttribute method, it works very
differently in IE compared to other browsers, you can simply assign to
element object properties instead e.g.
element.id = 'someId';
element.onclick = someJavaScriptFunction;
or in your case you like want e.g.
element.onclick = new Function ("evt", topArray[j]["a_onclick"]);


--

Martin Honnen
http://JavaScript.FAQTs.com/
Reinhold Schrecker
Guest
 
Posts: n/a
#3: May 22 '06

re: JavaScript/DOM: Can't set onclick-attribute for <a>'-tags in IE


>or in your case you like want e.g.[color=blue]
> element.onclick = new Function ("evt", topArray[j]["a_onclick"]);[/color]

....works fine.

Thanks a lot for your fast support.
Reinhold

VK
Guest
 
Posts: n/a
#4: May 22 '06

re: JavaScript/DOM: Can't set onclick-attribute for <a>'-tags in IE



Martin Honnen wrote:[color=blue]
> Reinhold Schrecker wrote:
>
>[color=green]
> > I am trying to generate a dynamic menu with JavaScript/DOM and have
> > problems to set the onclick-attribute for my <a>-elements.[/color]
>
> With the HTML DOM don't use the setAttribute method, it works very
> differently in IE compared to other browsers, you can simply assign to
> element object properties instead e.g.
> element.id = 'someId';
> element.onclick = someJavaScriptFunction;
> or in your case you like want e.g.
> element.onclick = new Function ("evt", topArray[j]["a_onclick"]);[/color]

As an addon: not to correct the solution (it doesn't need to) but to
clarify the situation with setAttribute/removeAttribute methods, as it
gives an impression of IE doing ("once again" :-) something wrong.

setAttribute/removeAttribute are methods to operate with the element
nodes (as reflected in the DOM Tree), not with DOM interface (as
provided to the script engine). This they have a rather narrow
application domain: when you need to change the DOM Tree itself (say if
you are preparing it for a tree walker). Respectively setAttribute
takes its argument as a plain vanilla text string and just add it as
nodeValue to the tree. It doesn't and it shouldn't to anyhow parse it,
DOM specs are very clear on it. So the way some browsers do allow you
to assign intrinsic handlers this way (over setAttribute) is little
convenience cheet-chat on specs. I personally welcome it (as any extra
convenience) but officially they shouldn't do it.

In the sample below you can click on "Test" to see that the dynamic
link indeed has attribute "ONCLICK" with value "alert('OK')" - but this
is just an attribute (not a handler) with /text/ value (not JScript
code).

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function demo(){
var p = document.createElement('P');
var a = document.createElement('A');
a.innerHTML = '<b>Click me</b>';
a.id = 'a01';
a.href = '#';
p.appendChild(a);
document.body.appendChild(p);
a.setAttribute('onclick', "alert('OK')");
}

window.onload = demo;
</script>
</head>

<body>
<p style="cursor:pointer"
onclick="alert(document.getElementById('a01').getA ttribute('ONCLICK'))">
Test</p>
</body>
</html>

Closed Thread