1

I know I'm doing something silly. But I just can't figure it out. I want to replace some text inside an tag immediately when the document loads. I know the function is executing. But it konks out at the getElementById statement and I don't know why. The output should say "New Junk". But instead always says "Original Stuff".

<html>
<script>
window.onload = filltip();

  function filltip() {
   alert('xyz');
   var id = 'qourl';
   var txt = 'New Junk';
   //alert('current html=' + document.getElementById(id).innerHTML);
   document.getElementById(id).innerHTML = '?<span class="classic">' + txt +   
'</span>';
  }

</script>
<body>

<a href='#' id='qourl'>Original Stuff</a>

</body>
</html>
0

1 Answer 1

7

Change your onload to this:

window.onload = filltip;

By writing window.onload = filltip(), you are actually calling filltip immediately, and using its return value as the onload function (in this case, undefined). In general, when you are assigning a function, you never want () on it unless the function very specifically returns another function.

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

1 Comment

Thank you very much. It doesn't make a lot of sense to me but I tried it and it works.

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.