3

This is just a simple javascript code to change display or background or just to see if I can change the css attribute using javascript at all.

This is based on an example.

But for some reason, I cannot change the attribute using the js function I defined myself.

HTML code

<div id="question"> testting javascript</div>
<button onclick="close()">Close</button>

JS code

function close(){
    document.getElementById("question").style.display = "none";
    document.getElementById('question').style.backgroundColor = "#f3f3f3";
}

The JSfiddle

The Full code

<div id="question"> testting javascript</div>
<button onclick="close()">Close</button>
<script>
function close(){
    document.getElementById("question").style.display = "none";
    document.getElementById('question').style.backgroundColor = "#f3f3f3";
}
</script>
8
  • Didi you use your function inside $('document').ready(function(){});? Commented Jan 16, 2017 at 2:32
  • 1
    @LuizHenrique that's jQuery, and OP isn't referencing jQuery anywhere. Commented Jan 16, 2017 at 2:32
  • uhhh no? because the example didn't use that Commented Jan 16, 2017 at 2:33
  • use a different name for close - because window.close is already a function - jsfiddle.net/jaromanda/5oc62qbe/12 Commented Jan 16, 2017 at 2:34
  • @JaromandaX changed close() to closed() but still not work Commented Jan 16, 2017 at 2:36

1 Answer 1

8

It may fail to work from 2 reasons:

  1. JSFiddle wraps all JavaScript code within an anonymous function by default, so your close() function is never exposed to the main scope.
  2. You chose the name close which is existing JavaScript method for closing the window: window.close.

The code below is working:

window.closeQuestion = function() {
  document.getElementById("question").style.display = "none";
  document.getElementById('question').style.backgroundColor = "#f3f3f3";
};

/*/ This is a way of having a full control over your JavaScript functions and events: /*/
document.getElementById('closeBtn').addEventListener('click', function() {
  closeQuestion();
});
#question {
  background : red;
}
<div id="question"> testting javascript</div>

<!-- This is lame: -->
<button onclick="closeQuestion()">Close</button>

<!-- This is better: -->
<button id="closeBtn">Close (better)</button>

You should handle events in JavaScript anyway. Using on* attributes is so last century. See addEventListener.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks. it is so last century for people who have experience. but I'm just starting to learn javascript that is why even the most basic confuses me. addEventListener and window.functionname <- I will look into this more
I would say it fails for both reasons you mentioned. If I change the name into something else, then I get an error log of a function that does not exist. That means before it did not find it, and assumed it is window.close function.
It's alright! Just keep that in mind while learning, so you can smoothly transition into writing a better code in the future! I recommend learning more about JavaScript scopes and Unobtrusive JavaScript. Good luck! ;-)
thanks. toddmoto site cannot be opened though
If the website doesn't work, there's also a great set of examples in the question What is the scope of variables in JavaScript? here on StackOverflow. It can be a bit confusing at first, so I'll drop a little hint: Everything you define in a global scope ends up under a window. variable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.