JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old March 14th, 2005, 06:31 PM
Dulcinea Dulcinea is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 23 Dulcinea User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 50 m 49 sec
Reputation Power: 0
Send a message via Yahoo to Dulcinea
Radio button

Hi all,

I face a problem using radio button. Here is my code:

Quote:

function delete_func(){

var value = false;



for ( counter = 0; counter < document.npi_status.my_project.length; counter++ ) {
if ( document.npi_status.my_project[counter].checked){
var my_value = document.npi_status.my_project[counter].value;
value = true;
}
}

if (!value){
window.alert('Please select one to delete');
return false;
}



}



i have no problem when there is more than 1 radio button.But, If there is only one radio button, the code cant work well as if i select the radio button, the alert window will come out. There will have no problem if there is more than 1 radio button. Can anyone help me to give your solutions?

Regards;
:: DuLCiNeA ::

Reply With Quote
  #2  
Old March 14th, 2005, 10:29 PM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
Hey Dulcinea! I don't know if this is the best way to go...but it'll keep you off the hook until someone provides a better solution.

Code:
 
function blabla(objRadio){
	var myValue;
	var found = false;
	if(objRadio.length == undefinded){  //only one radio button
		 if(objRadio.checked){
			 myValue = objRadio.value;
			 found = true;
		 }
	}
	else{ //multiple radio buttons
		for (var counter = 0; counter < objRadio.length; counter++ ){
			if (objRadio[counter].checked){
				myValue = objRadio[counter].value;
				found = true;
			}
		}
	}
	if (!found){
		window.alert('Please select one to delete');
		return false;
	}
}


This should cover the case when you have only one radio button. I never post anything that I don't test myself, but...considering it's 1:30 AM of a Tuesday (for me), I'll leave it up to you!.
Remember to use the code tags, to make the code more readable!
And a final piece of advice: try to write functions with parameters (like this one). It will make it posible to reuse the code somewhere else independently of the form and its elements name!

Good Luck!

ANibal.

PS: where are you from? I mean...do you speak spanish (I assume that from your nick)?

Reply With Quote
  #3  
Old March 15th, 2005, 12:07 AM
Dulcinea Dulcinea is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 23 Dulcinea User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 50 m 49 sec
Reputation Power: 0
Send a message via Yahoo to Dulcinea
Hi Anibal;

Thanks for your code as it works well.

if(objRadio.length == undefinded) <<-- It got an error until i realized there was a typo there. It should be undefined. I jus need to do a correction here for those who want to refer Anibal's code.
Thanks for your advice. I really appreciate it!

p/s: i know nuthing about spanish. I got my nick from reader's digest.

Regards;
:: DuLCiNeA ::

Reply With Quote
  #4  
Old March 15th, 2005, 09:50 AM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
Hey Dulcinea! Right....undefined! Unfortunatly I didn't see it when I typed it....I would have saved you sometime!!
I'm happy it worked.

Good Luck girl!

Anibal.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Radio button


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway