1

I'm new to develope in javascript, I'm sure this is a banal question and probably it has a very simple answer but I'm stuck and it seems I can't figure it out in any way. Can anyone give me an explaination on this?

here's my code:

    function getURLParameter(name) {
      return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
    }
    myvar = getURLParameter('searchbox');
    document.getElementById('field').value = myvar;

I keep on receiving a "Uncaught TypeError: Cannot set property 'value' of null" in the last line at document.getElementById('field').value = myvar;

Why? where I'm getting wrong?

FabioEnne
  • 720
  • 12
  • 37
  • 1
    Well, what does `console.log(myvar)` tell you? – msfoster Feb 26 '15 at 15:59
  • 5
    Either there is no element with the ID 'field', or it hasn't been loaded yet. See [Why does jQuery or a DOM method such as getElementById not find the element?](http://stackoverflow.com/q/14028959/1529630) – Oriol Feb 26 '15 at 15:59
  • 1
    @msfoster it gives me the right value.. – FabioEnne Feb 26 '15 at 16:04
  • @Oriol I try to add somehting like "window.onload" or document.addEventListener('DOMContentLoaded', function() but it seems to don't work...can you show me your idea by modifying my given script? – FabioEnne Feb 26 '15 at 16:15
  • @FabioEnne See http://jsfiddle.net/w934ua7w/ – Oriol Feb 26 '15 at 16:18
  • @Oriol great! thanks very much! it worked just fine! – FabioEnne Feb 26 '15 at 16:46

0 Answers0