An if statement in a popupwindow

Need abit of help with this...

Page 1 of 1

2 Replies - 745 Views - Last Post: 10 April 2010 - 07:43 PM

#1 Riv  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 21-January 10

An if statement in a popupwindow

Posted 03 April 2010 - 04:08 PM

Well I am doing an external javascript and I just need alittle bit of help....
This is an if statement that I am calling to check if the user has checked anything in the radio button form , if the user did not select anything then well I would display a popup window telling the user that they did not select anything. For some reason when I dont select anything it has the other if statment Thank you form selected, take a look at my function.

function menuSelections(notChecked) {
       if (document.mortgage.propLocation.selectedIndex == -1) {
         notChecked +="  <li>You didnt Select any of the options!\n</li>";
         }

   else {
         notChecked +="   <li>Thank you location Selected</li>";
       }

        document.mortgage.propLocation.value = notChecked;
        return notChecked;
     }



Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: An if statement in a popupwindow

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4364
  • View blog
  • Posts: 12,182
  • Joined: 18-April 07

Re: An if statement in a popupwindow

Posted 03 April 2010 - 09:57 PM

I am not sure I follow this code you have here. But to check a radio button you have to loop through the radio button group and see if they are checked with the "checked" property of each button. selectedIndex is for listboxes and comboboxes. So for instance, if you have 3 radio buttons linked up in a group called "radioGroup" then you would have to use something similar to this...

var somethingSelected = false;
for (var i = 0; i < radioGroup.length; i++) {
     if (radioGroup[i].checked) {
          somethingSelected = true;
     }
}

if (!somethingSelected) {
     alert("You need to select one of the radio buttons!");
}



Notice how we loop through the radio button group using a for loop and check if any of them are checked using the "checked" property. If you find one checked, then you will set the somethingSelected variable to true. Otherwise none are selected and your alert will be shown because somethingSelected will still be false.

This method is also how you check checkboxes for values. Make sure you test for situations where you might have one radio button or one checkbox. In such situations you will just access the one radio/checkbox singly because there will be no array of buttons/boxes.

Hope you understand what I am telling you. Enjoy! :)
Was This Post Helpful? 1
  • +
  • -

#3 Riv  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 21-January 10

Re: An if statement in a popupwindow

Posted 10 April 2010 - 07:43 PM

Thank you very much this was very helpful
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1