2

Sooooo, I'm having a JS problem. I'm fairly new to web development so I'm not entirely sure what I'm doing wrong with my JS.

//create script for #mainHeading focus @ pageload -- heading 1
    window.onload = function() {
        var mainHeading = document.getElementById("mainHeading");
        mainHeading.style.textShadow = '0px 0px 0px #fff'
    }

//create script for #subHeading focus @ pageload -- heading 2
    window.onload = function() {
        var subHeading = document.getElementById("subHeading");
        subHeading.style.textShadow = '0px 0px 0px #fff'
    }

So basically I wrote the function for my first heading to "focus" when the page loads. Then I decided to add a second heading with the same function. Now when I load my page only the second heading loads and not both.

Why is this??

4 Answers 4

7

Because you're overwriting the first function. Don't set the property like that... use addEventListener or equivalent instead:

window.addEventListener("load", function () {
    // First event handler
});
window.addEventListener("load", function () {
    // Second event handler
});

This method allows you to register as many event handlers as is necessary. Note that very old versions of Internet Explorer don't support it though, so you may need to feature detect it and use attachEvent in those old IE versions.

Currently, you are simply setting the value of a property. Consider the following code, which is effectively equivalent:

var obj = {};
obj.x = 10;
obj.x = 20;
console.log(obj.x); // 20
Sign up to request clarification or add additional context in comments.

Comments

3

You're replacing the onload function.

The right solution would be to add an event listener using addEventListener, they're additive :

window.addEventListener('load', function1);
window.addEventListener('load', function2);

Comments

1

Instead of replacing your onload function, simply add instructions to it. Nice oldschool and therefore cross-browser compatible:

window.onload = function() {
    var mainHeading = document.getElementById("mainHeading"),
         subHeading = document.getElementById("subHeading");
    mainHeading.style.textShadow = '0px 0px 0px #fff';
    subHeading.style.textShadow = '0px 0px 0px #fff';
}

Good Luck!

Comments

0

You're overwriting the method on the second one. You need to merge them together:

window.onload = function() {
    var mainHeading = document.getElementById("mainHeading"),
        subHeading = document.getElementById("subHeading");

    mainHeading.style.textShadow = subHeading.style.textShadow = 
                                                  '0px 0px 0px #fff';
};

You can also do what the other answers did as well. That's seems nice too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.