-1

I'd like to make a list about facts. When I click on the button the first fact should be replaced by the second and so on. But it doesn't work. Here is my code:

var facts = ['1. ', '2. ', '3. ', '4. '];

var print = document.getElementById('myfacts');
print.innerHTML = facts[0];

var i = 1;

function go() {
  'use strict';
  print.innerHTML = facts[i];
  i = (i + 1) % (facts.length);
}

Here is my HTML:

<section id="fakten">
  <div class="facts">
    <h1>My 4 facts</h1>
    <script src="facts.js"></script>  
    <p id="myfacts" class="myfacts"></p>
    <a id="click" href="index.html" onclick="go();">Next</a>
  </div>
</section>

But it doesn't work. Could you help me?

Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
Mike
  • 27
  • 9
  • It’s a dupe of [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/q/14028959/4642212). – Sebastian Simon Sep 01 '17 at 11:21

5 Answers5

3
<script src="facts.js"></script> 

U are mising a = sign.

Also change href="index.html" to href="#".

var facts = ["1. ", "2. ", "3. ", "4. "];

    var i = 1;
    var print = document.getElementById('myfacts');

    print.innerHTML = facts[0];

    function go() {
        "use strict";
        print.innerHTML = facts[i];
        i = (i + 1) % (facts.length);
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<section id="fakten">
    <div class="facts">
        <h1>My 4 facts</h1>
        <script src="facts.js"></script>  
        <p id="myfacts" class="myfacts"></p>
        <a id="click" href="#" onclick="go();">Next</a>
    </div>
</section>
Flowen
  • 395
  • 1
  • 2
  • 16
  • no thats not the problem, its only a mistake in the question – Mike Sep 01 '17 at 11:16
  • 1
    Why do you have 2 `href` in your anchor tag? – Hassan Imam Sep 01 '17 at 11:22
  • @HassanImam Thanks for pointing it. Just updated. Anyways that had nothing to do with the answer. – Flowen Sep 01 '17 at 11:24
  • @Flowen i believe pointing `href` to "#" is what made the answer correct, as @Mike mentioned missing "=" was a typo – Nemani Sep 01 '17 at 11:27
  • I think it is accidentally working here as the code snippet include the js in the end of body. And the mentioned js is not even being referenced. – Hassan Imam Sep 01 '17 at 11:30
  • The reason is that userscripts operate in a sandbox ("isolated world"), and onclick operates in the target-page scope and cannot see any functions your script creates. – DanteTheSmith Sep 01 '17 at 11:51
1

My guess is that you're looking for an element before it's created. Change the order:

<p id="myfacts" class="myfacts"></p>
<script src="facts.js"></script>

Another option is to use (some changes will be needed):

document.addEventListener("DOMContentLoaded", function(event) { 
  // you code
}

Beyond that you need to change from href="index.html" to href="#", or prevent default behavior.

Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
1

I think its because of href="index.html", instead use href="#"

var facts = ["1. ", "2. ", "3. ", "4. "];

    var i = 1;
    var print = document.getElementById('myfacts');

    print.innerHTML = facts[0];

    function go() {
        "use strict";
        print.innerHTML = facts[i];
        i = (i + 1) % (facts.length);
    }
<section id="fakten">
    <div class="facts">
        <h1>My 4 facts</h1>
        <script src="facts.js"></script>  
        <p id="myfacts" class="myfacts"></p>
        <a id="click" href="#" onclick="go();">Next</a>
    </div>
</section>
Nemani
  • 778
  • 5
  • 12
1

If you open a console, does it say go is not defined? Define it like so:

go = function() {
        print.innerHTML = facts[i];
        i = (i + 1) % (facts.length); 
} 

Now it's bound globally and working.

var facts = ["1. ", "2. ", "3. ", "4. "];

    var i = 1;
    var print = document.getElementById('myfacts');

    print.innerHTML = facts[0];

    go = function() {
        "use strict";
        print.innerHTML = facts[i];
        i = (i + 1) % (facts.length);
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<section id="fakten">
    <div class="facts">
        <h1>My 4 facts</h1>
        <script src="facts.js"></script>  
        <p id="myfacts" class="myfacts"></p>
        <a id="click" href="#" onclick="go();">Next</a>
    </div>
</section>

Polluting the global namespace like this is terrible practice—don't do that. This is for test purposes.

The root of the problem is explained in the answers to this question: Uncaught ReferenceError: function is not defined with onclick

There is an entire list of bad stuff happening in this code. You might consider the following:

  1. Why href to index.html?
  2. Why use the link at all? Use html5 button for that.
  3. Why is use strict in the function? (I don't see a reason why.)
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
DanteTheSmith
  • 2,937
  • 1
  • 16
  • 31
  • thank you @Cody Gray I just learned more about how to work with SO to produce better-styled answers! – DanteTheSmith Sep 01 '17 at 12:50
  • 1
    Yeah, Stack Snippets are basically like our own version of JSFiddle, with the advantage that they work *directly* in the post (and the links never go down!). You're welcome. – Cody Gray - on strike Sep 01 '17 at 13:02
0

I was able to get this to execute by wrapping the javascript in a callback for the DOMContentLoaded listener and making a few changes. This way the DOM is full loaded before the script executes. It ensures that the #myfacts and #click elements are accessible before the script runs. I also changed the <a> into a <button> to avoid being redirected to index.html and moved the onclick registration into the script itself.

Markup:

<section id="fakten">
    <div class="facts">
        <h1>My 4 facts</h1>
        <script src="./js.js"></script>  
        <p id="myfacts" class="myfacts"></p>
        <button id="click">Next</button>
    </div>
</section>

Javscript:

document.addEventListener("DOMContentLoaded", function(event) {
    var facts = ["1. ", "2. ", "3. ", "4. "];
    var i = 1;

    var print = document.getElementById('myfacts');
    var button = document.getElementById('click');

    button.onclick = function () {
      "use strict";
      print.innerHTML = facts[i];
      i = (i + 1) % (facts.length);
    }

    print.innerHTML = facts[0];
});
kylethebaker
  • 58
  • 1
  • 4