-3

I'm trying to change the href of my link based on whats in the textbox with id="serie". As you can see, Firefox tells me that el is null. What did go wrong here?

(Upper part is Page, Middle part the debug-console and the lower part is my source code)

<html>
    <head>
        <script>
            var el = document.getElementById('serie');
            var lnk = document.getElementById('link');
            el.onchange = el.onkeyup = function() {lnk.href = "http://bs.infinibrain.net/" + userInput + ".xml";};
        </script>
    </head>
    <body>
        <h1>Anleitung</h1>
        1. <a href="index.xml" target="_blank">Hier</a> die id eurer Serie suchen (Leider sind noch nicht alle aufgelistet)<br/>
        2. dann id hier eingeben: <input id="serie" /><br/>
        3. und <a href="#" id="link">hier</a> die links raussuchen <br/>
    </body>
</html>
LostPhysx
  • 3,573
  • 8
  • 43
  • 73

5 Answers5

3

You need use window.onload, because your script runs before the DOM has been loaded

window.onload = function () {
   // your code 
}

or put script before </body>

Example

Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
2

You are trying to access the element even before its loaded. Place your script at the end of your body.

    <html>
    <body>
        <h1>Anleitung</h1>
        1. <a href="index.xml" target="_blank">Hier</a> die id eurer Serie suchen (Leider sind noch nicht alle aufgelistet)<br/>
        2. dann id hier eingeben: <input id="serie" /><br/>
        3. und <a href="#" id="link">hier</a> die links raussuchen <br/>
        <script>
            var el = document.getElementById('serie');
            var lnk = document.getElementById('link');
            el.onchange = el.onkeyup = function() {lnk.href = "http://bs.infinibrain.net/" + userInput + ".xml";};
        </script>
    </body>
</html>

Also you didnot define userInput, it should be el.value:

lnk.href = "http://bs.infinibrain.net/" + el.value + ".xml";
Zee
  • 8,420
  • 5
  • 36
  • 58
  • @Paedow I updated my answer. Works fine See [HERE](http://jsfiddle.net/Lypn61tr/). – Zee May 26 '15 at 08:36
1

Your script is being evaluated before the page is loaded, which means that the html DOM is not yet available. What you need to do is add you script in the load event of the window. This event is triggered when the page is fully loaded:

<script>
    window.addEventListener("load", function () {
        var el = document.getElementById('serie');
        var lnk = document.getElementById('link');
        el.onchange = el.onkeyup = function() {lnk.href = "http://bs.infinibrain.net/" + userInput + ".xml";};
    });
</script>
Elian Ebbing
  • 18,779
  • 5
  • 48
  • 56
0

You need to use document DOMContentLoaded or window.onload.

window.onload = function() {}

OR

document.addEventListener("DOMContentLoaded", function() {})
Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60
0

userInput variable is undefined.

And move your script below your html content. It will be like this

<html>
<head>

</head>
<body>
    <h1>Anleitung</h1>
    1. <a href="index.xml" target="_blank">Hier</a> die id eurer Serie suchen (Leider sind noch nicht alle aufgelistet)<br/>
    2. dann id hier eingeben: <input id="serie" /><br/>
    3. und <a href="#" id="link">hier</a> die links raussuchen <br/>

<script>
var el = document.getElementById('serie');
var lnk = document.getElementById('link');
el.onchange = function() {
  userInput = el.value;
  lnk.href = "http://bs.infinibrain.net/" + userInput + ".xml";};
    </script>
  </body>
</html>

BTW changing question after answers are given is not allowed.

Mustaghees
  • 506
  • 6
  • 16