user3842025
user3842025

Reputation: 75

How to know if radio button with specific id is checked to use in if else statement

I want to do is to show the dialogbox if all the inputs are empty. And if the Yes radio button is checked the other inputs with asterisk should be filled also or else the dialog will show up.

My problem is even i checked the radio button with a value of Yes the dialog wont show up.

current output: http://jsfiddle.net/5kcsn/245/

script:

$("#dialog-confirm").dialog({
    resizable: false,
    height: 140,
    modal: true,
    autoOpen: false,
    buttons: {
        "Close": function () {
            $(this).dialog("close");
        }
    }
});

$('#presentEmploymentInformationDialog').click(function () {
    if (   
    ($("input[name='employed']:checked").val() == undefined) && 
    (
    ($('#employed_v1[value=Yes]:checked').val() == 'true') ||
    ($("input[name='presentEmployed']:checked").val() == undefined || $('#officeName').val() == '' || $('#officeAddress').val() == '' || $('#jobPosition').val() == '' || $('#noYearsInPosition').val() == '' || $('#statusOfEmployment').val() == '' )
    )
       )
    {
        $("#dialog-confirm").dialog("open");
    }else{
        exit;
    }
});

Upvotes: 1

Views: 600

Answers (3)

Aimee Borda
Aimee Borda

Reputation: 842

you can use following code to see if a checkbox is selected

$("selector").is(":checked")

Here is example in jsfiddle

Upvotes: 2

Milan and Friends
Milan and Friends

Reputation: 5610

Try this

$('#presentEmploymentInformationDialog').click(function() {
  if($('input[name="employed"]:checked').val() === 'Yes' && $('.row input[type="text"], .row select').val() === '') {
     $('#dialog-confirm').dialog("open");
  }
  else{
     $('.show-page[data-page="Eligibilty_info"]').trigger('click');
  }
});

FIDDLE

Upvotes: 0

charlietfl
charlietfl

Reputation: 171669

you are over complicating your selectors and causing at least one test that can never work.

Example:

$('#employed_v1[value=Yes]:checked').val() == 'true')

This can never evaluate true. In the selector you say the value is Yes but then try to compare it to 'true'. Id's must be unique so there can only be one #employed_v1 so just use that as the selector.

You might want to use something like this for the radios which cuts down some code

$('[name="employed"]:checked').length /* if 0 neither is checked */

For the text inputs you could use filter() on them

 var invalidTextFields= $('#officeName, #officeAddress,#jobPosition.....').filter(function(){
        return this.value=='';
 }).length;

The main difficulty in debugging what you have is there are too many || chained together with suspect selectors mixed in

Upvotes: 0

Related Questions