views:

52

answers:

3

Why does this work only if i call the alert function ? After i close the alert box, the color changes. If I comment the line with the alert command then nothing happens.

    function setLinkColor(el) 
    {
        var color =  getStyle(document.getElementById(el.id), "color");
        alert(el.id);
        document.getElementById("content").style.borderColor = color;
    }
+1  A: 

Bobby Blue :Problem with JavaScript is it does not shout for errors even when its unhappy, so we can't know for sure whats the problem unless we use external tools like Firebug. Install Firebug, Check for any other errors using it. As musicfreak said alert cannot impact on execution of statement(s).

Xinus
A: 

No idea why it doesn't work, but it could be something to do with your getStyle(element) function.

Don't know why you would use that when you could just as easily do this:

function setLinkColor(el) {
  var c = el.style.color;
  document.getElementById("content").style.borderColor = c;
}
Vincent McNabb
i am getting the style from a stylesheet
Bobby Blue
+2  A: 

Because alert paused the execution.

Without pause, #content might be not available yet.

Cheng
what can i do then ?
Bobby Blue
You could run the script only after the page has loaded, using body.onLoad or placing the script at the end of the document body.
Vincent McNabb
You should find out why #content is unavailable. Then change the execution order of your code.
Cheng