/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] checkbox, one or the other

i am trying to create a checkbox to say if a visitor is here 1st time or not (i need to use checkbox over radio), but i cant seem to get it to uncheck the opposite option. Can anyone point out what I am doing wrong?
Thanks
Bandion

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>
<meta http-equiv=”Content-type” content=”text/html;charset=UTF-8″ />
<title>

</title>
<script type=”text/javascript”>

/* <![CDATA[ * /
function sumbitForm() {
if (document.forms[0].salutation.options[0].selected == “”
|| document.forms[0].name.value == “”) {
window.alert(“You must enter all form data.”);
return false;
}
else
return true;
}
[COLOR=”Red”]function opposite() {
if (document.forms[0].newVisit.checked == true) {
document.forms[0].returnVisit.checked = false);
}
else
if (document.forms[0].returnVisit.checked == true) {
document.forms[0].newVisit.checked = false);
}
}[/COLOR]

/* ]]> */

</script>
</head>
<body>
<form action=”” method=”get” enctype=”application/x-www-form-urlencoded”>
<p>Please select your gender.</p>
<input type=”radio” name=”gender”
value=”male” checked=”checked” />Male<br />
<input type=”radio” name=”gender”
value=”female” />Female<br />
<br />
<select name=”salutation”>
<option value=”Mr”>Mr.</option>
<option value=”Mrs”>Mrs.</option>
<option value=”Ms”>Ms.</option>
<option value=”none”>None</option>
</select>
<input type=”text” name=”firstName” size=”15″ />
<input type=”text” name=”lastName” size=”25″ />
<br />
<br />
[COLOR=”Red”]<input type=”checkbox” name=”visit” checked=”checked”
value=”newVisit” onmouseup=”opposite()”/>First time visitor<br />
<input type=”checkbox” name=”visit”
value=”returnVisit” onmouseup=”opposite()” />Returning visitor<br />
<input type=”button” name=”submit” value=”Submit” />[/COLOR]

</body>
</html>

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineMay 15.2009 — <form>

<input type="checkbox" name="visit" checked="checked" value="newVisit" onmouseup="opposite(this.form.visit[1])"/>First time visitor<br />

<input type="checkbox" name="visit" value="returnVisit" onmouseup="opposite(this.form.visit[0])" />Returning visitor<br />

<input type="button" name="submit" value="Submit" >

</form>

<script>

function opposite(other)

{

other.checked = this.checked;

}

</script>
Copy linkTweet thisAlerts:
@BandionauthorMay 15.2009 — I could not get this to work with the modifications... I still get an error on the page.

Bandion
Copy linkTweet thisAlerts:
@Sterling_IsfineMay 16.2009 — I could not get this to work with the modifications... I still get an error on the page.

Bandion[/QUOTE]
Yes that wasn't right but this is tested:
[CODE]<html>
<body>
<form>
<input type="checkbox" name="visit" checked="checked" value="newVisit" onclick="opposite(this, this.form.visit[1])"/>First time visitor<br />
<input type="checkbox" name="visit" value="returnVisit" onclick="opposite(this, this.form.visit[0])" />Returning visitor<br />
<input type="button" name="submit" value="Submit" >
</form>
<script type='text/javascript'>

function opposite(a,b)
{
b.checked = a.checked ? false : b.checked;
}

</script>
</body>
</html>[/CODE]


Here's an example of an object that sets up multiple checkbox groups to simulate radio buttons
[CODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Checkbox Groups Act As Radio Buttons (Don't ask)</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name='f1' action='#'>
<p>
<input type=checkbox name='cb1'>
<input type=checkbox name='cb1'>
<input type=checkbox name='cb1'>
<input type=checkbox name='cb1'> Uncheckable
<p>--------------
<p>
<input type=checkbox name='cb2'>
<input type=checkbox name='cb2'>
<input type=checkbox name='cb2'>
<input type=checkbox name='cb2'> Not Uncheckable
</form>
<script type='text/javascript'>

function Cb2Rb( setRef, canUncheck )
{
this.boxGroup = setRef;

for( var i=0, len=setRef.length; i<len; i++ )
setRef[ i ].onclick=( function(inst, idx, cu){return function(){inst.scan(idx, cu)}} )(this, i, canUncheck);

this.scan=function(index, uc)
{
if( this.boxGroup[ index ].checked )
{
for(var i=0, g=this.boxGroup, len=g.length; i<len; i++)
if( i != index )
g[i].checked = false;
}
else
if( !uc )
this.boxGroup[ index ].checked = true;
}
/*28432953637269707465726C61746976652E636F6D*/
}

new Cb2Rb( document.forms.f1.cb1, true );
new Cb2Rb( document.forms.f1.cb2, false );

</script>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@BandionauthorMay 17.2009 — Thank you, it worked great....

now for the fun part...

can you explain what the function means?

[COLOR="Red"] i do not know what the ? and : mean[/COLOR]

function opposite(a,b)

{

b.checked = a.checked ? false : b.checked;

}


Thanks again

Bandion
Copy linkTweet thisAlerts:
@Sterling_IsfineMay 18.2009 — Thank you, it worked great....

now for the fun part...

can you explain what the function means?

[COLOR="Red"] i do not know what the ? and : mean[/COLOR]

[/QUOTE]


https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/Conditional_Operator
Copy linkTweet thisAlerts:
@BandionauthorMay 18.2009 — Thanks again... this explained it so i understood...

Bandion
Copy linkTweet thisAlerts:
@Y_LessMay 18.2009 — Out of interest, why can't you use radio buttons? This is exactly what they're designed for and what people expect them to do. If you start changing the functionality of check boxes people are going to wonder what's going on; not to mention the problems introduced with javascript disabled.
Copy linkTweet thisAlerts:
@BandionauthorMay 19.2009 — it is a school assignment... we have to use one of each type.. radio, check, selection and text...


I had the assignment complete but I wanted one less step, ie the user not having to uncheck an option. I just could not figure out how to do it...

I thought I was on the right track.. but turned out I wasnt.

Bandion
×

Success!

Help @Bandion spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...