Rahul Singh
Rahul Singh

Reputation: 1632

Skipping if condition in jquery

I am using following code snippter on button click event. Issue is if I don't select radio button than it gives me alert undefined but if I use same condition that is if I equate value of same in if condition than it escape condition. From below code I am not getting alert("Select action to perform."); as this if condition is skipped.

 $('#actionbutton').click(function(){ 

        alert('HI SANKALP');
        var manageradiorel = $('input[name="managerelradio"]:checked').val();
        var parentid = <?php echo $parentid; ?>;
        var childid = $('#managechild').val();
        var sgid = <? echo $_GET["s"]; ?>;
        var relationship = $('#childsgrel').val();
        alert(manageradiorel);
        if(manageradiorel == "undefined"){
            alert("Select action to perform.");
            return false;
            }
        alert("Next time");

     });

Upvotes: 1

Views: 450

Answers (3)

Erik Philips
Erik Philips

Reputation: 54638

in Javascript there are two optimal ways to determine if a variable exists. One is more descriptive than the other.

  1. if (manageradiorel)
  2. if (typeof manageradiorial == "undefined")

Update:

If you have access to Jquery 1.6 or higher, I highly recommend using the jQuery prop() method instead of val();

Upvotes: 2

Hussein
Hussein

Reputation: 42818

try the following

if(manageradiorel){...}

Upvotes: 1

sapptime
sapptime

Reputation: 430

Try changing if(manageradiorel == "undefined") to if(manageradiorel === undefined)

Upvotes: 1

Related Questions