Click to See Complete Forum and Search --> : How to make this little script correct?


kariba
05-15-2003, 04:41 PM
Hello all,

I have this script:
<script language=javascript><!--
document.getElementById('sort1').innerHTML = "<img src=images/sort_asc.gif border=0>";
//--></script>

and it gives me error, that object is null... How can I write that so it's compatible with browsers and doesn't produce errors?

The id object is span tag.

Thank you very much.

Mike

pyro
05-15-2003, 04:43 PM
It looks good to me. Which browser are you having trouble with? That should work with all that support getElementById...

Vladdy
05-15-2003, 04:44 PM
Where do you have this line of script in relation to the span element???

General note (but that is not the cause of your problem) - using innerHTML is now a bad style and can be a compatibility broblem in the future.
If you need to add one element to another use DOM functions:

with(document.getElementById('span1').appendChild(document.createElement('img')))
{ src = 'someimage.gif';
alt = 'image description';
}

kariba
05-15-2003, 05:59 PM
Hi guys,

thank you for your reply. I am using IE, but I need it to work in netscape, too. is it possilbe?

I generate that little script on the server and then write it into page, it is in the body tags.

I like that child solution, I will try it with that.

Thanks alot,

Mike

Vladdy
05-15-2003, 06:14 PM
where is it in relation to the span element you are trying to modify???

kariba
05-15-2003, 10:02 PM
the span is on page just with id, and if there is an action on the page(when the user sorts table) the specific picture(asc, or desc) is assigned to the span.

Is this clearer?

Thanks a lot for your help.

Mike

Vladdy
05-16-2003, 05:52 AM
Nope "there is" does not reveal the possible reason to your problem. Is it before or after that is what I'm trying to get to...
If you put your script before the span element, getElementById will fail since there is no span element with this id at that point.

pyro
05-16-2003, 08:09 AM
You should probably put your script in an onload event. This will make your script run after the entire page has loaded. Something like this:

<script language="javascript" type="text/javascript">
function doSomething() {
//your code here
}
</head>
<body onload="doSomething();">