By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,948 Members | 1,587 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,948 IT Pros & Developers. It's quick & easy.

function call executing only one line onclick

Tarantulus
100+
P: 114
Hi guys,

My JS is terrible so forgive me, but I'm having trouble with a function to hide/show divs, the code is as follows:

Expand|Select|Wrap|Line Numbers
  1. var state
  2. state=0;
  3. function edit(){
  4. if(state==0){
  5. state=1;
  6. document.getElementById('editor_area').style.display='inline';
  7. document.getElementById('display_area').style.display='none';
  8.  
  9.  
  10.         document.getElementById('jump').focus()
  11. };
  12. else{
  13. state=0;
  14. document.getElementById('editor_area').style.display='none';
  15. document.getElementById('display_area').style.display='inline';
  16.  
  17. };
  18. };
however when I call edit() (button onclick) it only performs the first show/hide action after resolving the if (for example state==1, would result in "state=0;
document.getElementById('editor_area').style.displ ay='none';" being actioned);

how can I fix this?
I hope this makes sense.

thanks in advance
May 21 '08 #1
Share this Question
Share on Google+
14 Replies


hsriat
Expert 100+
P: 1,654
Try this and tell me if there's any success..
Expand|Select|Wrap|Line Numbers
  1. var state = false;
  2. function edit() {
  3.     document.getElementById('editor_area').style.display = state ? 'none' : '';
  4.     document.getElementById('display_area').style.display = state ? '' : 'none';
  5.     state = !state;
  6.     if (!state) document.getElementById('jump').focus();
  7. }
May 21 '08 #2

Tarantulus
100+
P: 114
Try this and tell me if there's any success..
Expand|Select|Wrap|Line Numbers
  1. var state = false;
  2. function edit() {
  3.     document.getElementById('editor_area').style.display = state ? 'none' : '';
  4.     document.getElementById('display_area').style.display = state ? '' : 'none';
  5.     state = !state;
  6.     if (!state) document.getElementById('jump').focus();
  7. }
No dice, the onclick doesn't activate anything now.
May 21 '08 #3

hsriat
Expert 100+
P: 1,654
No dice, the onclick doesn't activate anything now.
ow.. it's hard to believe that it got even worse..

There's no space in disp lay, make sure it's not causing the problem.

And replace the empty quotes with inlay.. just in case you have provided display:none as default in css.
May 21 '08 #4

acoder
Expert Mod 15k+
P: 16,027
You're using a single equal, instead of double equals:
Expand|Select|Wrap|Line Numbers
  1. var state = false;
  2. function edit() {
  3.     document.getElementById('editor_area').style.display = state ? 'none' : '';
May 21 '08 #5

hsriat
Expert 100+
P: 1,654
You're using a single equal, instead of double equals:
Expand|Select|Wrap|Line Numbers
  1. var state = false;
  2. function edit() {
  3.     document.getElementById('editor_area').style.display = state ? 'none' : '';
...umm.... state is itself a boolean. I'm not comparing it.
May 21 '08 #6

Tarantulus
100+
P: 114
Thanks for this, but it's still misbehaving...

now the "display_area" div is disappearing, but the "editor_area" div wont appear :S.

this is really frustratiing, it's not even throwing any errors that I could investigate!

Thanks again
May 21 '08 #7

hsriat
Expert 100+
P: 1,654
Thanks for this, but it's still misbehaving...

now the "display_area" div is disappearing, but the "editor_area" div wont appear :S.

this is really frustratiing, it's not even throwing any errors that I could investigate!

Thanks again
There was a space in display of editor area code line too.. Did you delete that?
May 21 '08 #8

Tarantulus
100+
P: 114
There was a space in display of editor area code line too.. Did you delete that?
yep, all cleaned up, the spaces have been removed and I've put 'inline' instead of ' '
May 21 '08 #9

hsriat
Expert 100+
P: 1,654
yep, it now reads as
Expand|Select|Wrap|Line Numbers
  1.  var state = false;
  2. function edit() {
  3.     document.getElementById('editor_area').style.display = state ? 'none' : 'inline';
  4.     document.getElementById('display_area').style.display = state ? 'inline' : 'none';
  5.     state == !state;
  6.     if (!state) document.getElementById('jump').focus();
  7. }
lol... that is funny... do u think I gave those spaces intentionally?


But anyhow, is this working now or not?...
This should definitely work. If it doesn't, then there's some other problem with the code.
May 21 '08 #10

acoder
Expert Mod 15k+
P: 16,027
...umm.... state is itself a boolean. I'm not comparing it.
Ah, I see, sorry about that.

@Tarantulus, post your HTML code, or a link.
May 21 '08 #11

Tarantulus
100+
P: 114
lol... that is funny... do u think I gave those spaces intentionally?
Of course not, I'm dumb, but not that dumb!
But anyhow, is this working now or not?...
This should definitely work. If it doesn't, then there's some other problem with the code.
you're right, it was a problem with my DIV positioning :)

Thanks for your help, really appreciate it.
May 21 '08 #12

acoder
Expert Mod 15k+
P: 16,027
lol... that is funny... do u think I gave those spaces intentionally?
There's a bug with the code tags. The fix has been in the pipeline for some time. Hopefully it'll come out of the tunnel soon...
May 21 '08 #13

acoder
Expert Mod 15k+
P: 16,027
you're right, it was a problem with my DIV positioning :)
Can you post the two versions, i.e. what exactly was wrong with the positioning?
May 21 '08 #14

hsriat
Expert 100+
P: 1,654
There's a bug with the code tags. The fix has been in the pipeline for some time. Hopefully it'll come out of the tunnel soon...
I know that... and that doesn't upset me.

But it sometimes causes confusions for the new users.

regards
May 21 '08 #15

Post your reply

Sign in to post your reply or Sign up for a free account.