user3737149
user3737149

Reputation: 33

how can i use if - else statement in jquery?

I am developing one project in asp.net c# . Here are two buttons in jquery yes or no . If I clicked the yes button then the "no" button will be hide and vice versa . Anyone please help me . ......

$(document).ready(function () {
    if("#btnYes").click(function () {
        $('#hdnYesNoAnswer').val('1');
    });
});

$(document).ready(function () {
    $("#btnNo").click(function () {
        $('#hdnYesNoAnswer').val('0');
    });
});
</script>

Upvotes: 1

Views: 222

Answers (3)

doctororange
doctororange

Reputation: 11810

jQuery's click handlers respond to click events. So you don't need to query if something was clicked; you just define what should happen when it is clicked, like this:

$(document).ready(function () {
  // when a button is clicked, hide it and show the other button
  $("#btnYes, #btnNo").click(function () {
    $("#btnYes, #btnNo").show();
    $(this).hide();
  });

  // when yes is clicked, set the answer to 1
  $("#btnYes").click(function () {
    $('#hdnYesNoAnswer').val('1');
  });

  // when no is clicked, set the answer to 0
  $("#btnNo").click(function () {
    $('#hdnYesNoAnswer').val('0');
  });
});

Upvotes: 4

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

This should work fine.

$(document).ready(function () {
    $("#btnYes").click(function () {
        $('#hdnYesNoAnswer').val('1');
    });

    $("#btnNo").click(function () {
        $('#hdnYesNoAnswer').val('0');
    });
});

Upvotes: 0

xdazz
xdazz

Reputation: 160883

You could even don't use if else.

$(function() {
  $("#btnYes,#btnNo").click(function () {
    // +true is 1, +false is 0
    $('#hdnYesNoAnswer').val(+(this.id == 'btnYes'));
  });
});

Upvotes: 3

Related Questions