PDA

View Full Version : checkboxes and buttons


scroots
11-04-2002, 09:03 PM
when i click a button how can i get a checkboxes name and redirect to a specific location based on the checkboxes name?
so a checkbox with the name 54 would redirect to http://myyy.com/funticon.htm?54

thanks in advance

scroots

beetle
11-04-2002, 10:43 PM
Didn't I answer that question here? (http://www.codingforums.com/showthread.php?s=&threadid=8479)

scroots
11-05-2002, 08:38 PM
that was one way, but for my slow pc, running pws there must be a better way instead of writing two lines of code with each checkbox.
its for a calender, i have a checkbox for each event and 1 edit button and 1 delete button.
you see where i`m coming from?

scroots

beetle
11-05-2002, 08:56 PM
I don't understand. This works just fine...<html>
<head>
<script>
function checkClick(e) {
var cb = (document.all) ? e.srcElement : e.target;
if (cb.type != "checkbox") return;
if (cb.checked)
if (confirm("Proceed?"))
top.location.href = 'blah.htm?'+cb.name;
else
return false;
}
</script>
</head>
<body onClick="checkClick(event);">
<form>
<input type="checkbox" name="1"><br>
<input type="checkbox" name="2"><br>
<input type="checkbox" name="3"><br>
<input type="checkbox" name="4"><br>
<input type="checkbox" name="5"><br>
<input type="checkbox" name="6"><br>
<input type="checkbox" name="7"><br>
<input type="checkbox" name="8"><br>
<input type="checkbox" name="9"><br>
<input type="checkbox" name="10"><br>
<input type="checkbox" name="11"><br>
<input type="checkbox" name="12"><br>
<input type="checkbox" name="13"><br>
<input type="checkbox" name="14"><br>
<input type="checkbox" name="15"><br>
</form>
</body>
</html>

scroots
11-05-2002, 09:08 PM
it works fine if you want to redirect when you check the box, but i would like to have it so i check the box and then depending on which button i press next (edit or delete) i get redirecte to delete.htm?checkboxname or edit.htm?checkboxname

understand?

scroots

beetle
11-05-2002, 09:17 PM
Yes, I understand. But what if the user checks more than one? Are you going to allow that?

Do you want to use a standard form? (ie. if 4 is checked, the url would look like 'delete.htm?4=on') or do you just want 'delete.htm?4' ?? It can be done either way....

scroots
11-05-2002, 09:30 PM
only one would be checked at a time, if more where checked it would alert and ask you to uncheck some boxes., its for my solo use only. the url would be if chekcbox 4 is checked delete.htm?4

thanks
scroots

beetle
11-05-2002, 09:58 PM
well, I suggest using radio buttons. They exist for the purpose of allowing only one per group to be selected.<html>
<head>
<title>Test</title>
<script>
function doClick(rGroup, l) {
var val = '';
for (var i=0; i<rGroup.length; i++)
if (rGroup[i].checked)
val = rGroup[i].value;
if (confirm("Proceed?"))
top.location.href = l + "?" + val;
}
</script>
</head>
<body>
<form>
<input type="radio" name="choice" value="1"><br>
<input type="radio" name="choice" value="2"><br>
<input type="radio" name="choice" value="3"><br>
<input type="radio" name="choice" value="4"><br>
<input type="radio" name="choice" value="5"><br>
<input type="radio" name="choice" value="6"><br>
<input type="radio" name="choice" value="7"><br>
<input type="radio" name="choice" value="8"><br>
<input type="radio" name="choice" value="9"><br>
<input type="radio" name="choice" value="10"><br>
<input type="radio" name="choice" value="11"><br>
<input type="radio" name="choice" value="12"><br>
<input type="radio" name="choice" value="13"><br>
<input type="radio" name="choice" value="14"><br>
<input type="radio" name="choice" value="15"><br>
<input type="button" value="Edit" onClick="doClick(this.form.choice, 'edit.htm');"><br>
<input type="button" value="Delete" onClick="doClick(this.form.choice, 'delete.htm');">
</form>
</body>
</html>

scroots
11-05-2002, 10:00 PM
thank you beetle, i will use radio buttons then, as it sounds a lot better.
thanks again
scroots

MacSP
11-05-2002, 10:27 PM
Bah. Giving up so soon?

The following code will give you your checkboxes, selected one at a time. Just replace the alert with a document.location or whatever function you want to use.


<HTML>

<HEAD>
<SCRIPT>
var myURL = "";
function changeURL(choice) {
myURL = "http://myyy.com/funticon.htm?" + choice.id;
boxes = document.getElementsByName("box");
var size= document.getElementsByName("box").length;
for (var i=0; i<size; i++) {
if (boxes[i] != choice) {
boxes[i].checked = false;
}
}
}
</SCRIPT>
</HEAD>

<BODY>
<FORM>
<INPUT type="checkbox" name='box' id='1' onclick="changeURL(this)">
<INPUT type="checkbox" name='box' id='2' onclick="changeURL(this)">
<INPUT type="checkbox" name='box' id='3' onclick="changeURL(this)">
<INPUT type="button" value="Click Me" onclick="alert(myURL);">
</FORM>
</BODY>

krycek
11-05-2002, 10:43 PM
um...

...just a thought...

One thing we as web designers/developers should do is try to keep to existing behaviours. For instance, people who have used computers know instinctively that you can drag windows around by dragging the title bar. If we change a behaviour, these people (talking about the not-very-computer-literate!) can easily get confused!

I personally think that, if you are using checkboxes, you should allow multiple selections - and hence multiple deletions at one go. The code is not complicated, and the script beetle has posted is a very good place to start from.

If you intend to use checkboxes that only allow one of them to be selected, then this is potentially confusing. The idea of most GUIs, be they Windows, MacOS, Linux, BeOs, etc. etc. etc. is that there are standard behaviours. I think radio buttons would be a better idea if you only want to enable one selection... but that raises the point, why not simply have a link or button next to each entry saying delete? It would be instantly obvious what is means, and very easy to code - especially if you are worried about two lines instead of one! :confused: (I don't know why, because you are not going to notice any speed different there!)

Just my two pence :D

::] krycek [::

beetle
11-05-2002, 10:49 PM
Originally posted by MacSP
Bah. Giving up so soon?MacSP. scroots' request CAN be handled with checkboxes, and better than what you have done here (you didn't give him the EDIT and DELETE buttons he needs). However, that aside, it is just plain wasteful to generate code/algorithms to treat an array of checkboxes as a radiobutton group, when rabiobutton groups exist for us to use with no javascript coding to make them do what they do.

Pesonally, I'd handle this as a GET method form, which would require almost no javascript, but that is not what scroots wanted.

MacSP
11-05-2002, 11:10 PM
Hey he asked how it could be done, not if it was a best coding practice.

If a user/customer says make it a checkbox, he'd end up doing it no matter how stupid it might be. Also, his original question says nothing about delete or edit, and I was responding to his initial statement.

I don't agree with checkboxes either, I just answered the question. Geez, defensive group here.

krycek
11-05-2002, 11:24 PM
MacSP... I was not intending to attack what you said in any way - I was simply trying to point out to scroots that his request may not be the best way of doing what he wanted :)

Also, sometimes it is best to tell a client what they want - after all, that is part of our job as web designers/developers ;) I have never found a client yet who has disagreed with my logic on such things when the time is taken to properly explain the reasoning to them :)

But hey, it may well be that scroots definitely needs the task doing in that way, and top marks to you for providing the script :D

::] krycek [::

beetle
11-06-2002, 12:26 AM
Originally posted by MacSP
Geez, defensive group here. I don't think so. Opinionated? Yes. :D

whammy
11-06-2002, 01:15 AM
scroots, it also makes no sense to me why you aren't using radio buttons.

checkboxes are used when you want people to enter multiple responses.

radio buttons are used when you only want the user to enter a single response.

After the most recent responses to this thread, I am really confused at to what you're actually trying to accomplish. Can you start from the beginning and just say it in English? (no code.) :D