473,378 Members | 1,469 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,378 software developers and data experts.

Radio button values

I'm trying to set up a script that checks to see how many radio buttons are set to Yes and how many are set to No.

I'm using PHP to generate all the radio buttons and name them, and as it goes through, it gives each one a numeric ID based on the order, and a name which is equal to what the option is for. At the end of the field, I have the submit button call the JavaScript function, and provide it with the maximum number of selected options and the total number of options displayed(as this will change based on who is logged in and setting things up).

Here is my code:

function keywordcheck(remaining, total){
count = 0
for(i = 1; i < total; ++i){
if (document.getElementById(i).getAttribute('checked' ) == 'checked'){
count++
} //if
} //for

if (count > remaining){
alert("You have selected too many keywords.\n You have chosen " + count + " keywords.\n Please Choose only " + remaining + " keywords")
return false
} else {
return true
}
}

I know the flaw is in the if statement. The radio button has 2 objects with the same ID, and the first one always has a value of Yes and the second, ignored one is marked as No. So...as it scans through, every time it checks the first ID it gets the value of Yes.

I'm brand new to JavaScript, so I know this is a really stupid question, but I'm not new to coding. I have a good amount of experience with PHP and C++, so I'm not new to the logic.

Any suggestions?
Mar 17 '07 #1
1 1600
Logician
210 100+
I'm using PHP to generate all the radio buttons and name them, and as it goes through, it gives each one a numeric ID based on the order,

if (document.getElementById(i).getAttribute('checked' ) == 'checked')
IDs cannot start with a digit, and all IDs in a document must be unique.
The checked property of a radio button is boolean (true or false only) and can and should be read directly rather than via getAttribute.
Mar 17 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Oscar Monteiro | last post by:
I Have to sets of Radio buttons like so: <input type="radio" name=p1 value=1> <input type="radio" name=p1 value=2> <input type="radio" name=p1 value=3> <br> <input type="radio" name=p2 value=1>...
3
by: ewitkop90 | last post by:
Here is my code: <SCRIPT> function transportchange(transport) { if (framenewinstall.Helpdesk.checked) framenewinstall.Helpdesk.checked=false; if (framenewinstall.CircuitNumber.checked)...
4
by: Jay | last post by:
I have a form used to submit data (no surprises there!). I'd like to be able to populate the same form with previously submitted data. The data lives in a database once submitted and using ASP I...
4
by: Jared | last post by:
Radio Button or Check Box and Event Procedures I need to insert either radio buttons or check boxes onto my form. I'm not sure which to use, or if there are other options. I am using the buttons...
8
by: crayfiss | last post by:
Hi, firstly I am a total freshie in all this. From what I have gathered on the web and this forum, I finally managed to get my form up. I have a set of radio buttons with values to it and a select...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.