Question

Focus on radio buttons

Asked by: lisa_devlin

Hi,

I'm using the following javascript to test if one of a set of radio buttons have been checked...

adv= -1
      for (i=0; i<form.advice.length; i++) {
      if (form.advice[i].checked) {
      adv=i
      }
      }
      if (adv == -1) {
      alert ('Are you an independant financial adviser? Please select yes or no')
form.advice.focus()
      return false
}

The actual validation part works fine but the focus() part seems to send the form off to the server instead of returning false. If I take out the focus() part, it returns false which is what I want it to do. How can I get the form to return false AND focus on the radio after validation?

Thanks!

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2000-09-12 at 05:45:07ID11255394
Tags

javascript

,

radio

Topic

JavaScript

Participating Experts
5
Points
50
Comments
25

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Focus on a radio button
    If a have a radio group. How do I focus on one of the radio buttons in the radio group.
  2. Focus
    How do I set a default focus on specific text box or listbox when opening an ASP page?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: dij8Posted on 2000-09-12 at 06:00:32ID: 4273830

The way I do this is to return false seperately from all other statements.  Using your example it would work something like:

for (x=1;x<2;x++) {
nogo = false;
adv= -1
for (i=0; i<form.advice.length; i++) {
if (form.advice[i].checked) {
adv=i
}
}
if (adv == -1) {
alert ('Are you an independant financial adviser? Please select yes or no')
form.advice.focus()
nogo = true;
break;
}
}

if (nogo) {return false}

The extra loop (x) is so that you can break out of multiple fields validation.

 

by: lisa_devlinPosted on 2000-09-12 at 06:05:05ID: 4273906

Hmm..i just gave that a try, and it didn't work..Form still got submitted.

 

by: CUTTHEMUSICPosted on 2000-09-12 at 06:14:16ID: 4273993

try putting return false first. Like this also don't forget the ;

adv= -1
                      for (i=0; i<form.advice.length; i++) {
                      if (form.advice[i].checked) {
                      adv=i
                      }
                      }
                      if (adv == -1) {
                      alert ('Are you an independant financial adviser? Please select yes or no')
                      return false;
                      form.advice.focus() ;
                      }

 

by: lisa_devlinPosted on 2000-09-12 at 06:17:56ID: 4274026

OK, that stops the form from submitting, but it still doesnt focus on the radio...

 

by: CUTTHEMUSICPosted on 2000-09-12 at 06:19:58ID: 4274043

Could you post your entire code. I'll try to fix it.

 

by: lisa_devlinPosted on 2000-09-12 at 06:21:06ID: 4274066

Im just using the code you gave me - ive taken out everything else and just validating that one field

 

by: CUTTHEMUSICPosted on 2000-09-12 at 06:24:02ID: 4274107

change
form.advice.focus();
to
documents.forms[0].advice.focus() ;

 

by: lisa_devlinPosted on 2000-09-12 at 06:28:06ID: 4274170

no difference, sorry :-(

 

by: CUTTHEMUSICPosted on 2000-09-12 at 06:47:01ID: 4274428

How about a link to the script.

 

by: lisa_devlinPosted on 2000-09-12 at 06:49:38ID: 4274488

I cant because its on our local server. Ok...all I am using is what you gave me just now. So i have..

<script language="javascript">
function chkFields() {
adv= -1
                      for (i=0; i<form.advice.length; i++) {
                      if (form.advice[i].checked) {
                      adv=i
                      }
                      }
                      if (adv == -1) {
                      alert ('Are you an independant financial adviser? Please select yes or no')
                      return false;
                      form.advice.focus() ;
                      }
}
</script>

Then:
form name="subscription" onSubmit="return chkFields()">

etc etc.

The focus works fine for all the other form elements. It just wont focus on the radio button.

 

by: lisa_devlinPosted on 2000-09-12 at 06:50:29ID: 4274506

Oh, as an aside. I can't get it to focus on any radio button in the form. Its not just this one.

 

by: CUTTHEMUSICPosted on 2000-09-12 at 06:59:22ID: 4274638

I'm not 100% sure but we might be trying to do something that is not possible. Atleast not using any of the above ways.

 

by: lisa_devlinPosted on 2000-09-12 at 07:03:10ID: 4274732

Maybe. The focus() works for every other form field. Apparently all form elements except "hidden" support it.

 

by: CUTTHEMUSICPosted on 2000-09-12 at 07:07:26ID: 4274794

Still Checking.

 

by: lisa_devlinPosted on 2000-09-12 at 07:17:53ID: 4274928

I cant because its on our local server. Ok...all I am using is what you gave me just now. So i have..

<script language="javascript">
function chkFields() {
adv= -1
                      for (i=0; i<form.advice.length; i++) {
                      if (form.advice[i].checked) {
                      adv=i
                      }
                      }
                      if (adv == -1) {
                      alert ('Are you an independant financial adviser? Please select yes or no')
                      return false;
                      form.advice.focus() ;
                      }
}
</script>

Then:
form name="subscription" onSubmit="return chkFields()">

etc etc.

The focus works fine for all the other form elements. It just wont focus on the radio button.

 

by: CUTTHEMUSICPosted on 2000-09-12 at 07:32:30ID: 4275165

to focus on a radio use
document.forms[0].advice[1].focus()">

where 1 is the number of the radio button you would like to focus on

 

by: lisa_devlinPosted on 2000-09-12 at 07:36:09ID: 4275269

Ok, sorry, that didn't work either. Hope you didn't waste many points finding that out ;-)

I think youre right and maybe we are trying to do the impossible...

 

by: CUTTHEMUSICPosted on 2000-09-12 at 07:39:01ID: 4275349

I wouldn't call it a waste. I got to the point were I wanted to know the answer too.
Good Luck

 

by: lisa_devlinPosted on 2000-09-12 at 07:40:32ID: 4275366

Thanks, and you...if i find out anything more i'll let you know.

 

by: dij8Posted on 2000-09-12 at 08:35:42ID: 4276508

I have a page that does this.  Check out my amazing work ;-)
http://www.royalmail.com/queenmother/ and then go to competition.

The code you need is in there. I can extract it if you can't find it exactly.

 

by: danilPosted on 2000-09-12 at 13:37:48ID: 4281011

your problem is that you are using onSubmit event. It does not prevent form from submission even if your data validation fails. Try using instead of "type=submit"
regular button,
<input type='button' value=submit onclick='chkFields()'>
 and then manually submit
your form
script language="javascript">
function chkFields() {
adv= -1
                      for (i=0; i<form.advice.length; i++) {
                      if (form.advice[i].checked) {
                      adv=i
                      }
                      }
                      if (adv == -1) {
                      alert ('Are you an independant financial adviser? Please select yes or no')

                      form.advice.focus() ;
                      return false;
                      }
else {
  document.form.submit();
 return;
}
}
</script>

 

by: naxPosted on 2000-09-12 at 19:05:18ID: 4283611

you overlooked one small detail:

form.advice.focus()

should be

form.advice[0].focus();

remember form.advice is an array containing the same number of radio buttons you have.

you didn't get the return false to work because there was an error before that.

when CUTTHEMUSIC reversed the order of the return and focus it only disabled the focus part since the return doesn't execute anything after it.

please try this, i hope it works.

 

by: dij8Posted on 2000-09-20 at 06:11:29ID: 4371370

Nothing seems to have happened here for a while so here's a fully working page.  You should be able to extract what you want.

<html>
<head>
<script language="JavaScript" type="text/javascript">
var b, buttonvalue, everythingok
function validateform(form) {
    var nogo = false
    for (i=1;i<2;i++) {
        if (!cycleButtons(form.whenborn)) {alert("Please select which day you think HM The Queen Mother was born on in 1900");nogo=true;form.whenborn[0].focus();break;}
        if (!cycleButtons(form.whenmarried)) {alert("Please select when you think The Queen Mother married HRH Duke of York");nogo=true;form.whenmarried[0].focus();break;}
        if (!cycleButtons(form.howmany)) {alert("Please select how many official engagements you believe HM The Queen Mother fulfilled in 1997");nogo=true;form.howmany[0].focus();break;}
        if (form.username.value == "") {alert("Please enter your name");nogo=true;form.username.focus();break;}
        else {textvalidator(form.username)}
        if (everythingok == false) {alert("Please enter a name with only letters");nogo=true;form.username.focus();break}
    }
    if (nogo) {
        return false;
    }
}
function cycleButtons(buttonNum) {
      buttonchecked = false
      for (b = 0; b < buttonNum.length; b++) {
            if (buttonNum[b].checked) {
                  buttonchecked = true;
            }
      }
      if (buttonchecked == false) {return false}
      else {return true}
}
function textvalidator(field) {
    //do text validation for input box
}
</script>
</head>
<body>
<form method="post" action="mynewpage.asp">
<p>1. HM The Queen Mother was born on which day in 1900?:<br>
<input type="radio" name="whenborn" value="3">3rd August<input type="radio" name="whenborn" value="4">4th August<input type="radio" name="whenborn" value="5">5th August</p>
<p>2. HM The Queen Mother married HRH Duke of York in which year?<br>
<input type="radio" name="whenmarried" value="21">1921<input type="radio" name="whenmarried" value="22">1922<input type="radio" name="whenmarried" value="23">1923</p>
<p>3. In 1997, HM The Queen Mother fulfilled how many official engagements?<br>
<input type="radio" name="howmany" value="37">37<input type="radio" name="howmany" value="44">44<input type="radio" name="howmany" value="58">58</p>
<p>Name: <input type="text" name="username" size="30"></p>
<input type="submit" name="submitbutton" value="Submit" onclick="return validateform(this.form)">
</form>
</body>
</html>

 

by: dij8Posted on 2000-11-23 at 01:11:50ID: 5524190

Lisa, was this ever resolved?  I gave code that works.  You never gave any feedback so I don't know if you need any other changes made.

If you found a solution here then accept the relevent comment as an answer.  We are here to help and leaving questions until they automatically get deleted won't help others if they want to use the PAQ's.

 

by: MoondancerPosted on 2002-01-28 at 18:28:27ID: 6762743

It's time to clean up this topic area and that means taking care of this question. Your options at this point are:
 
1. Award points to the Expert who provided an answer, or who helped you most. Do this by clicking on the "Accept Comment as Answer" button that lies above and to the right of the appropriate expert's name.
 
2. PAQ the question because the information might be useful to others, but was not useful to you. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.
 
3.  Ask Community Support to help split points between participating experts.  Just comment here with details.
 
4.  Delete the question because it is of no value to you or to anyone else.  To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.
 
If you elect for option 2, 3 or 4, just post comment with details here and I'll take it from there.  We also request that you review any other open questions you might have and update/close them.  Display all your question history from your Member Profile to view details.
 
PLEASE DO NOT AWARD THE POINTS TO ME.
 
____________________________________________
 
 
 
Hi Experts:
 
In the event that the Asker does not respond, I would very much appreciate your opinions as to which Expert ought to receive points (if any) as a result of this question.  Likewise, you can also suggest that I PAQ or delete the question.
 
Experts, please do not add further "answer" information to this question.  I will be back in about one week to finalize this question.
 
Thank you everyone.
 
Moondancer :)
Community Support Moderator @ Experts Exchange

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...