Forum Moderators: open

Message Too Old, No Replies

this.innerHTML not woring in Internet Explorer?

Passing object reference to funcion, IE doesn'T do anything

         

Garfield

3:55 pm on Dec 24, 2005 (gmt 0)

10+ Year Member



I'm generating a list of spans with anchor tags inside on the fly by a javascript. Each of these span's anchors has an onlick event which calls SetValue(this);

The function SetValue(obj) is supposed to set a textfield to the inner HTML part of the clicked span. I do this by accessing the object by this.innerHTML. This works flawlessly in Firefox, Opera and Konqeror. Only Internet Explorer won't do anything.

My spans look like this:

<span class="foo"><a href="#" onclick="SetValue(this);">My value 1</a></span>

How can I access the HTML part of the span's anchor tag? Basically I wouldn't need the anchor tag, since they are no real links, but I added them, because I don't want to do the hover effect with javascript, and IE doesn't support hover for other elements than anchor tags.

Thanks in advance!

Bernard Marx

1:26 am on Dec 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Seems OK for me:


<script type="text/javascript">
function SetValue(elm)
{
alert(elm.innerHTML);
elm.innerHTML += " rabbits";
}
</script>
<span class="foo"><a href="#" onclick="SetValue(this); return false;">My value 1</a></span>

Garfield

5:35 pm on Dec 25, 2005 (gmt 0)

10+ Year Member



Thanks for your reply!

The script you posted works for me, too. But I guess there is a difference between your script and mine that keeps IE from showing the innerHTML part.

My script generates the spans on the fly like this:

locations = array("foo", "bar", "foobar", "barfoo");


for (i = 0; i < locations.length; i++) {
buffer = buffer + '<span><a href="#" onclick="setValue(this);">'+locations[i]+'</a></span>';
}
document.getElementById('dropdownlist').innerHTML = buffer;

dropdownlist now contains a list of spans. I guess IE cannot understand that it is supposed to add 'this' as a literal string rather than evaluating it.

How can I rewrite the function, that 'this' is replaced by locations[i]? I can't seem to get it working because of the apostrophes.

Thanks

Bernard Marx

10:00 pm on Dec 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



locations = [blue]A[/blue]rray("foo", "bar", "foobar", "barfoo");
buffer = "";

for (k = 0; k < locations.length; k++)
buffer = buffer + '<span><a href="#" onclick="setValue(this);">'+locations[k]+'</a></span>\n';

alert(buffer);

This produces the desired string (the strings wrapped in tags).

What do you mean by this?:

...it is supposed to add 'this' as a literal string rather than evaluating it.

It produces wrappings like:

<span><a href="#" onclick="setValue(this);">foo</a></span>

This is OK, isn't it. this isn't evaluated.

God Jul!

Bernard Marx

10:11 pm on Dec 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How can I rewrite the function, that 'this' is replaced by locations[i]?

Why would you want to do that?

this
carries more information.