longtone Posted July 10, 2009 Share Posted July 10, 2009 I am trying out this very simple script and getting errors. function newtext(o) { o.innerHTML="Hey! I have changed!"; setTimeout("moretext(o)",1000); } function moretext(o) { o.innerHTML="I just change with the time!"; } The first line works fine, but after 1 second i get the error: " o is not defined" If I replace o with it's value in the second function: function moretext(o) { document.getElementById('id').innerHTML="I just change with the time!"; } It works. I'm confused ??? Quote Link to comment Share on other sites More sharing options...
.josh Posted July 11, 2009 Share Posted July 11, 2009 what are you passing to newtext? Quote Link to comment Share on other sites More sharing options...
haku Posted July 11, 2009 Share Posted July 11, 2009 You're only showing us the functions. You aren't showing us how they are called. We can't tell you why they aren't working if you don't show us how they are called. Quote Link to comment Share on other sites More sharing options...
longtone Posted July 18, 2009 Author Share Posted July 18, 2009 what are you passing to newtext? o = document.getElementById('id') Quote Link to comment Share on other sites More sharing options...
longtone Posted July 18, 2009 Author Share Posted July 18, 2009 You're only showing us the functions. You aren't showing us how they are called. We can't tell you why they aren't working if you don't show us how they are called. <div id = "id"> <p>abc</p> </div> <div> <p><a href="#" onclick="newtext(document.getElementById('id')); return false" >def</a> </p> </div> Quote Link to comment Share on other sites More sharing options...
haku Posted July 19, 2009 Share Posted July 19, 2009 Unfortunately, you can't pass references to elements like that, which is where your problem is coming from. Or at least you can't pass them twice - it seems to work for the first time. But the problem is easy to fix - use this: function newtext(eid) { var o = document.getElementById(eid) o.innerHTML="Hey! I have changed!"; setTimeout("moretext(eid)",1000); } function moretext(eid) { var o = document. getElementById(eid) o.innerHTML="I just change with the time!"; } Then call it with this: <a href="#" onclick="newtext('id'); return false" >def</a> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.