473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

javascript in forms

*** post for FREE via your newsreader at post.newsfeed.com ***

hello,

i am trying to create a login page to my website. there are the usual
fields for name and password, but i want 2 more: a checkbox that a new user
will check, and then another text input that will appear if the box is
checked. is it possible to have this happen dynamically? i.e. the 2nd
password box will appear if it is checked, but it will disappear if it is
unchecked. thanks

andrew
-----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----

Jul 20 '05 #1
4 3203

"Andrew Clark" <la*****@hotmail.com> wrote in message
news:Xn*******************@208.33.61.211...
*** post for FREE via your newsreader at post.newsfeed.com ***

hello,

i am trying to create a login page to my website. there are the usual
fields for name and password, but i want 2 more: a checkbox that a new user will check, and then another text input that will appear if the box is
checked. is it possible to have this happen dynamically? i.e. the 2nd
password box will appear if it is checked, but it will disappear if it is
unchecked. thanks

andrew
-----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----


Andrew, I know you could have a layer hidden and show it upon clicking the
checkbox, but I've seen this done on other sites and think there may be a
better way. But for this way just create a layer with it's visibility
default set to hidden, and on you checkbox add an onclick event that passes
it's value (checked or not) to a function that will show or hide the layer
based on it. That'll get the job done but I'd wait for someone with more
experience with this to post something. Hopefully they do cause I'm going to
use it too.
Jul 20 '05 #2
Try this:

<html>
<head>

<style>
..showBoxOff {border: solid 0px #000000; width: 100px;
visibility:hidden;}
..showBox {border: solid 0px #000000; width: 200px; paddig: 10px;
visibility:visible;}
</style>

<script language="JavaScript">
function validate() {
if (document.loginForm.newUserBox.checked)
document.all.newUser.className="showBox"

else
document.all.newUser.className="showBoxOff"
}
</script>
</head>

<body>

<form name="loginForm">
username: <input type="text" name="user"><br>
password: <input type="password" name="pass"><br>
new user: <input type="checkbox" name="newUserBox"
onClick="validate()">
</form>

<div class="showBoxOff" id="newUser">

<form>
new field: <input type="text">
</form>
</div>

</body>
</html>
Jul 20 '05 #3
Andrew Clark wrote:
hello,

i am trying to create a login page to my website. there are the usual
fields for name and password, but i want 2 more: a checkbox that a new user
will check, and then another text input that will appear if the box is
checked. is it possible to have this happen dynamically? i.e. the 2nd
password box will appear if it is checked, but it will disappear if it is
unchecked. thanks

andrew


<body onload="setVisibility('additionalInfoId', false);">
<form name="loginForm">
<input type="text" name="userName" id="userNameId" />
<input type="password" name="userPass" id="userPassId" />
<input type="checkbox" name="newUser" id="newUserId"
onclick="toggleVisibility(this);" />
<input type="text" name="additionalInfo" id="additionalInfoId" />
</form>
<script type="text/javascript">
function isDefined(item) {
return (typeof item != 'undefined');
}
function setVisibility(elementId, displayState) {
if (document.getElementById) {
var element = document.getElementById(elementId);
if (element && isDefined(element.style) &&
isDefined(element.style.display)) {
if (displayState) {
element.style.display = 'inline';
} else {
element.style.display = 'none';
}
}
}
}
function toggleVisibility(chkbox) {
var id = chkbox.form.additionalInfo.id;
setVisibility(id, chkbox.checked);
}
</script>

Works in Netscape 4.x and Opera 6.05 (by simply keeping the input element
visible all the time), IE6SP1, Mozilla and Opera 7.11 (by toggling the
visibility of the input element).

By starting the display style in the default state (in this case 'inline') and
turning it off only when the browser is capable of doing so, you've guaranteed
that the input element will only be set to display 'none' in browsers that are
capable of turning it back on again.

It's coded specific to 'inline' elements, you could make it more generic to
handle both 'block' and 'inline' elements by passing that rather then a
boolean. In fact, you could pass both a style property and it's value and make
it even more generic:

function setStyle(id, styleProperty, styleValue) {
if (document.getElementById) {
var element = document.getElementById(id);
if (element && isDefined(element.style) &&
isDefined(element[styleProperty])) {
element[styleProperty] = styleValue;
}
}
}

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #4
*** post for FREE via your newsreader at post.newsfeed.com ***

Grant Wagner <gw*****@agricoreunited.com> wrote in
news:3F***************@agricoreunited.com:
Andrew Clark wrote:
hello,

i am trying to create a login page to my website. there are the usual
fields for name and password, but i want 2 more: a checkbox that a
new user will check, and then another text input that will appear if
the box is checked. is it possible to have this happen dynamically?
i.e. the 2nd password box will appear if it is checked, but it will
disappear if it is unchecked. thanks

andrew


<body onload="setVisibility('additionalInfoId', false);">
<form name="loginForm">
<input type="text" name="userName" id="userNameId" />
<input type="password" name="userPass" id="userPassId" />
<input type="checkbox" name="newUser" id="newUserId"
onclick="toggleVisibility(this);" />
<input type="text" name="additionalInfo" id="additionalInfoId" />
</form>
<script type="text/javascript">
function isDefined(item) {
return (typeof item != 'undefined');
}
function setVisibility(elementId, displayState) {
if (document.getElementById) {
var element = document.getElementById(elementId);
if (element && isDefined(element.style) &&
isDefined(element.style.display)) {
if (displayState) {
element.style.display = 'inline';
} else {
element.style.display = 'none';
}
}
}
}
function toggleVisibility(chkbox) {
var id = chkbox.form.additionalInfo.id;
setVisibility(id, chkbox.checked);
}
</script>

Works in Netscape 4.x and Opera 6.05 (by simply keeping the input
element visible all the time), IE6SP1, Mozilla and Opera 7.11 (by
toggling the visibility of the input element).

By starting the display style in the default state (in this case
'inline') and turning it off only when the browser is capable of doing
so, you've guaranteed that the input element will only be set to
display 'none' in browsers that are capable of turning it back on
again.

It's coded specific to 'inline' elements, you could make it more
generic to handle both 'block' and 'inline' elements by passing that
rather then a boolean. In fact, you could pass both a style property
and it's value and make it even more generic:

function setStyle(id, styleProperty, styleValue) {
if (document.getElementById) {
var element = document.getElementById(id);
if (element && isDefined(element.style) &&
isDefined(element[styleProperty])) {
element[styleProperty] = styleValue;
}
}
}

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...pt/1.3/referen
ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a.../dhtml_referen
ce_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html


thanks! this is exactly what i'm looking for.

andrew
-----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gowhera Hussain | last post by:
Use This for Learning Only .... Do Not Try To Act Smart HACKING WITH JAVASCRIPT Dr_aMado Sun, 11 Apr 2004 16:40:13 UTC This tutorial is an overview of how javascript can be used to bypass...
3
by: bhanubalaji | last post by:
hi, I am unable to disable the text(label) in javascript..it's working fine with IE,but i am using MOZILLA.. can any one help regarding this.. What's the wrong with my code? I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.