Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 2nd, 2011, 11:44 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select boxes/radiobuttoms hybrid

Hi all,

I want to create a group of select boxes where just 1 of them can be selected at any time (just as radiobuttoms) but that the possibility to select none of them is kept.

In the following example there are three rows with a pair of select boxes each. I want each pair to behave as described above: if 0 is selecetd 3 can't, and both 0 and 3 can be deselected at will.



Code:
<html>
<head>

<script>
function reCheck(){
var k = this.name;
if(k<3){document.forms["myform"][k+3].checked = false;}
    else{document.forms["myform"][k-3].checked = false;}
}
</script>

</head>
<body>
<form name = "myform" action="dothis.php" method="post">
<table  id="MyTable">
<?php
for($i=0;$i<=2;$i++)
{
$j = $i + 3;
echo "<tr>";
echo "<td>";
echo "<input type=\"checkbox\" name=\"".$i."\" onclick = \"reCheck()\" />".$i;
echo "</td>";
echo "<td>";
echo "<input type=\"checkbox\" name=\"".$j."\" onclick = \"reCheck()\" />".$j;
echo "</td>";
echo "</tr>";
}
?>
</table>
</form>
</body>
</html>
By the way, the box 4 can't be checked.

Can you help me with this?

Thanks in advance,

Omar
Reply With Quote
  #2 (permalink)  
Old December 3rd, 2011, 01:50 AM
Registered User
Points: 65, Level: 1
Points: 65, Level: 1 Points: 65, Level: 1 Points: 65, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well I think your javascript is not correct. And your question is not clear to me cuz i would have help you to solve the problem.
__________________
The easy boi.
Reply With Quote
  #3 (permalink)  
Old December 3rd, 2011, 05:48 AM
Imar's Avatar
Wrox Author
Points: 66,542, Level: 100
Points: 66,542, Level: 100 Points: 66,542, Level: 100 Points: 66,542, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,118
Thanks: 65
Thanked 1,381 Times in 1,361 Posts
Default

Maybe this solution using jQuery puts you on the right track?

http://imar.spaanjaars.com/562/creat...ns-with-jquery

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #4 (permalink)  
Old December 3rd, 2011, 07:10 AM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much Stiward and Imar. First of all you may have realized that I'm a total beginner and any help is welcome.

@Imar: I visited your page before posting here. Your solution is very nice and it worked perfect for me until I sorted the table. This table is sortable and hiddable, implemented according to (http://www.tablefixedheader.com/). Why when the table is sorted the functionality is lost (IE 8)?


@Stiward: Sorry if I wasn't clear before, I will try to be clearer now. For two select boxes I want these properties:
- The two can't be checked at the same time (like radiobuttons).
- They can be unchecked (not like radiobuttons).
I'm not surprised my code is wrong.

Thanks to both of you. I look forward.

Regards,

Omar
Reply With Quote
  #5 (permalink)  
Old December 3rd, 2011, 08:44 AM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi all,

I have passed the name of the checkbox as an argument to the javascript function and the checkboxes became responsive at least, but the pattern of activation is not the expected. Don't get what it is going on here. There are some alert messages to track if the detection of the checkbox by the javascript is correct, and it is but from there on the problematic behaviour begins. Here you have,

Code:
<html>
<head>

<script>
function reCheck(k){
if(k<3){
	alert(k+3+''+document.forms["myform"][k+3].checked)

	document.forms["myform"][k+3].checked = false;

	alert(k+3+''+document.forms["myform"][k+3].checked)
		}
    else{	
	alert(k-3+''+document.forms["myform"][k-3].checked)

	document.forms["myform"][k-3].checked = false;

	alert(k-3+''+document.forms["myform"][k-3].checked)
		}
	}

</script>

</head>
<body>
<form name = "myform" action="dothis.php" method="post">
<table  id="MyTable">
<?php
for($i=0;$i<=2;$i++)
{
$j = $i + 3;
echo "<tr>";
echo "<td>";
echo "<input type=\"checkbox\" name=\"".$i."\" onclick = \"reCheck(".$i.")\" />".$i;
echo "</td>";
echo "<td>";
echo "<input type=\"checkbox\" name=\"".$j."\" onclick = \"reCheck(".$j.")\" />".$j;
echo "</td>";
echo "</tr>";
}
?>
</table>
</form>
</body>
</html>
Looking forward,

Omar
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementation of ECIES for Hybrid Encryption harkeerat BOOK: Beginning Cryptography with Java 6 March 11th, 2011 11:59 AM
Dynamic check boxes and select lists bsheridan Classic ASP Basics 1 March 17th, 2006 04:39 AM
Multi-Select List Boxes and Calculations flrzeus BOOK: Expert One-on-One Access Application Development 0 December 5th, 2005 09:43 AM
Updating Records in db using select boxes realgone_ Classic ASP Databases 1 September 28th, 2004 02:23 PM
Popup menus over <select> boxes tdaustin Javascript How-To 6 July 7th, 2004 06:30 PM



All times are GMT -4. The time now is 05:51 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.