The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

One for the Javascript Wizards

I know how you all love my challenges, (see http://discuss.joelonsoftware.com/default.asp?design.4.395296.22 ), so I have a new one for you. The Javascript wizards might find this a lot easier than us mere mortals.

I have created a widget!

http://u2do.com/shared/widget.php

Clicking any radio button in each row should total all rows and display in the form field labelled "Total Percentage". This [amazingly for me] works.

My problem is that I wish to disable radio buttons which would cause the Total to go above 100%. For example:

Clicking 50% on Percentage 1, should disable all radio buttons greater than 50%  in Percentages 2,3,4 and 5. Clicking 20% on Percentage 2 would then disable all radio buttons greater than 30% in Percentages 3,4 and 5.

I've got a funny feeling this isn't possible, and that I should be approaching this particular problem from a different angle. If you have any suggestions for this you can still score points!

Big thanks to anybody who can help!
Gavin Laking Send private email
Wednesday, October 11, 2006
 
 
After var total, add var offIndex.

var offIndex = (100 - total)/10

This gives you a digit 0 to 10.
Then loop through each array of radio input tags.

for (...) {
    if (i >= offIndex) {
      db.a[i].disabled = true;
    }

Don't forget to add a button to reset the form and clear values.
Nonymous
Wednesday, October 11, 2006
 
 
Disable all the inputs in a row with a checked input to prevent the user from changing thier mind and potentially messing up the form.
Nonymous
Wednesday, October 11, 2006
 
 
Sorry,
if (i >= offIndex) {
should be:
if (i > offIndex) {

I'm doing this in my head and I do make mistakes.
Nonymous
Wednesday, October 11, 2006
 
 
Holy heck! Fantastic, thank you! You're a star!
Gavin Laking Send private email
Wednesday, October 11, 2006
 
 
Some slight improvements:

You have the same number of radio buttons on each row; use 1 loop instead of 5(?)

for(){
    db.a[i]...
    db.b[i]...
etc}

Set the disabled property directly...

  if (i >= offIndex) {
      db.a[i].disabled = true;
  }

becomes

      db.a[i].disabled = (i >= offIndex);

No longer necessary, but you could use the conditional operator to set the text colour:

  if (total > 100)
    {
      db.f.style.color = "FF0000";
      db.f.value = total;
    }
  else
    {
      db.f.style.color = "000000";
      db.f.value = total;
    }

becomes

  db.f.style.color = (total > 100 ? "FF0000" : "000000");
  db.f.value = total;
Justin Send private email
Thursday, October 12, 2006
 
 
Thanks Justin, I've made these optimizations to my source (but not the demonstration widget I posted here). For anybody else that is doing something similar, I found that the values were not being POSTed for some reason. That is to say that $_POST['radio_button_name'] didn't hold a value.

I corrected this by using Javascript to write the value into a hidden field.
Gavin Laking Send private email
Tuesday, October 24, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz