Jump to content

Exit Or Abort Function In Js?


Recommended Posts

So here is my routine. I want to abort the script inside the first if statement, so that when you click once, the div is displayed, and when you click a 2nd time, it hides. But this doesnt work if you reset x equal to 0, because it just keeps the div displayed. And if you dont reset x to 0, the 3rd click (if you wanted to show the div again), wont do anything. Any ideas?<script language="javascript">var x = 0;function showBody() {if(x == 1) { document.getElementById('body').style.display='none'; x=0;//NEED EXIT OR ABORT FUNCTION HERE............................................... }if(x == 0) { document.getElementById('body').style.display='block'; x = 1;}}</script><div onclick="showBody();">click here</div><div id="body" style="display:none;">this is the hidden body</div>

Link to post
Share on other sites
Just use return.function showBody() {if(x == 1) {document.getElementById('body').style.display='none';x=0;return;}...
what does return do? Ive never really been familiar with this, except the fact it can return a value. what does return alone do?
Link to post
Share on other sites

It exits the function immediately. Doesn't matter if you're in a loop or whatever. Just BANG and you're out. Control passes to the statement that follows the statement that called the function in the first place.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...