Solved

Facebook + Javascript + Hide - Show field Toggle = Headache

Posted on 2010-09-04
9
589 Views
Last Modified: 2012-06-27
Hello all,

i would like to show or hide a text field on a form in html in a facebook app when I click a checkbox.  I know the code to set whether a checkbox is true/false is as follows and I was hoping there would be something similarly simple to hide or show a textbox:

    document.getElementById('CheckAnswerA').setChecked(false);

Any help you can offer would be greatly appreciated. Many thanks, Colin.

0
Comment
Question by:Canders_12
  • 3
  • 3
  • 3
9 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
From here : http://developers.facebook.com/docs/fbjs#dom_objects

I see we may use setStyle :


document.getElementById("texboxID").setStyle("display", "none");

Open in new window

0
 
LVL 2

Expert Comment

by:filosof
Comment Utility
You need something like in the attached code snippet.

Additional notes:
In some cases maybe it makes sense to disable/enable the textarea as well - to suppress any input from sending to server etc..
...
<div>
<input type="checkbox"
 onclick="document.getElementById('mytext').style.display=(this.checked)?'':'none'">
</div>
...
<div>
<textarea id="mytext" style="display: none;"></textarea>
</div>
...

Open in new window

0
 

Author Comment

by:Canders_12
Comment Utility
excellent :)

is it easy just to do the reverse and show the textfield?

many thanks, Colin.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
to show use block instead none

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Expert Comment

by:filosof
Comment Utility
leakim971
No, using "block" is not correct for all cases - better to use empty string ("") !

why:
1. "" will also show the text field
2. sometimes you need another value instead of "block" - that is specified by CSS classes of element etc.. - "bock" will override this feature, "" - not

Canders_12
my example does both: hides and shows


....style.display = (this.checked) ? '' : 'none';
or
....style.display = (this.checked) ? 'none' : '';

Open in new window

0
 

Author Closing Comment

by:Canders_12
Comment Utility
Beautiful :)
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Thanks for the points!
0
 

Author Comment

by:Canders_12
Comment Utility
filosof,

sorry, you definitely deserve points here :(

is it against rules to pop up the same question to ensure you also get some points? sorry, i was rushing and didnt read everyones post.  Sorry dude.
0
 
LVL 2

Expert Comment

by:filosof
Comment Utility
I do not know how to adjust - just ask some moderator etc.  - no matter
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now