javascript - JavasScript function undefined -
i've got html element declared so:
<div id="taskimage" runat="server"> <a href="javascript:void(0);" onclick="switchviews('div<%# eval("proid") %>', 'one');"> <img id='imgdiv<%# eval("proid") %>' alt="click show/hide tasks" border="0" src="..."/> </a> </div>
and javascript function switchviews declared post-html such:
function switchviews(obj, row) { var div = document.getelementbyid(obj); var img = document.getelementbyid('img' + obj); if (div.style.display == "none") { div.style.display = "inline"; img.src = "../../images/icons/delete.png"; } else { div.style.display = "none"; img.src = "../../images/icons/add.png"; } }
when click on html element, thrown js error saying "object exepcted", , in google chrome script debugger says switchviews undefined. why think switchviews undefined , how go fixing it? record have tried javascript declared before html , switchviews call in href attribute ends same.
edit : clear up, post-html , pre-html mean before , after write out html elements. post like
<div> <!-- html stuff --> <div> <script type="text/javascript"> <!-- javascript --> </script>
and pre reverse of that
question
at point <a href="javascript:void(0);" onclick="switchviews(... etc
encountered browser, function switchviews
exist or undefined?
answer
the code defining switchviews
appears after, @ time switchviews
undefined.
solution
place <script>
before rest of html or attach event listener in javascript instead of inline. can done via addeventlistener
or using jquery , may require loop on htmlelements of interest.
Comments
Post a Comment