-1

my radio button is checked but i want it is onclicked, because i have a javascript function that using on.click function

is there any posible method or examples?

<label style="margin-left:177px;">Order Type
<label style="color:red;">*</label>:</label>

<input style="margin-left:20px;" type="radio" id="orang" class="dua" name="ORDER_TYPE" value="NEW" required <?php echo ($result["ORDER_TYPE"] === "NEW")?"checked" : ""; ?>/>
<label style="padding-right:10px;">New Registration</label>

<input type="radio" id="kucinga" class="dua" name="ORDER_TYPE" value="UPGRADE/MIGRATE"  required <?php echo ($result["ORDER_TYPE"] === "UPGRADE/MIGRATE")?"checked" : ""; ?>/>
<label>Upgrade/Migrate Existing</label>

<input style="margin-left:10px;"type="radio" id="kucingb" name="ORDER_TYPE" value="VAS" class="dua" required <?php echo ($result["ORDER_TYPE"] === "VAS")?"checked" : ""; ?>/>

JS :

$('input[name="ORDER_TYPE"]').on('click', function() {
    if ($(this).val() == 'NEW') {
        $('#textboxes').show();
    } else {
        $('#textboxes').hide();
    }
});
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
  • 2
    What do you mean `i want it is onclicked`? – Jeremy Thille May 25 '17 at 06:42
  • im using this function but this function is work when user is clicked on radio button `$(function() { $('input[name="ORDER_TYPE"]').on('click', function() { if ($(this).val() == 'NEW') { $('#textboxes').show(); } else { $('#textboxes').hide(); } }); });` but in my coding above i only can checked the radio button and its show the radio button checked but the onclick function doesnt work – Lokman Hakim May 25 '17 at 06:44
  • change click to change .. it will work – daremachine May 25 '17 at 06:51
  • 1
    you code working good [`fiddle`](https://fiddle.jshell.net/ebe05ztu/) .`id=textboxes` is missing in your html – prasanth May 25 '17 at 06:53
  • what u mean change click to change? – Lokman Hakim May 25 '17 at 06:55
  • that id=textboxes is just to to hide my html input only i think it doesnt important to show it here its mean when the radio button is clicked there is show/hide function on those belonging that id but on my coding above its only checked not onlicked so the function doesnt work – Lokman Hakim May 25 '17 at 06:57

4 Answers4

2

It's good to use .is() to determine if checkbox is checked.

// my hide logic function
$.fn.hideTextareaOrNot = function() {
    if($(this).is(':checked') && $(this).val() == 'NEW') {
    $('#textarea').hide();
  }
  else {
    $('#textarea').show();
  }
}

// on change listener
$('input[name="check"]').change(function() {
    $(this).hideTextareaOrNot();
});

// on page load init
$('input[name="check"]').each(function(v) {
    $(this).hideTextareaOrNot();
});

DEMO

daremachine
  • 2,678
  • 2
  • 23
  • 34
0

Change the event to 'change':

$('input[name="ORDER_TYPE"]').on('change', function() { 
    if ($(this).val() == 'NEW') { 
        $('#textboxes').show(); 
    } else { 
        $('#textboxes').hide(); 
    } 
});
DaFois
  • 2,197
  • 8
  • 26
  • 43
0

jQuery(".label").on('click', function(e){
  myFun();
})

function myFun(){
  console.log('Check console');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label style="margin-left:177px;">Order Type
                <label style="color:red;">*</label>:</label>

                <input style="margin-left:20px;" type="radio" id="orang" class="dua" name="ORDER_TYPE" value="NEW" required <?php echo ($result["ORDER_TYPE"] === "NEW")?"checked" : ""; ?>/>
                <label class="label" for="orang" style="padding-right:10px;">New Registration</label>

                <input type="radio" id="kucinga" class="dua" name="ORDER_TYPE" value="UPGRADE/MIGRATE"  required <?php echo ($result["ORDER_TYPE"] === "UPGRADE/MIGRATE")?"checked" : ""; ?>/>
                <label class="label" for="kucinga">Upgrade/Migrate Existing</label>

                <input style="margin-left:10px;"type="radio" id="kucingb" name="ORDER_TYPE" value="VAS" class="dua" required <?php echo ($result["ORDER_TYPE"] === "VAS")?"checked" : ""; ?>/>
0

Before I will answer ... Please stop experimenting with your labels, simply use:

<label for="input_ID">text</label>

Now possible solution would be:

const allradios = Array.from(document.getElementsByClassName("dua"));
allradios.map((radio) => {
  radio.addEventListener('click', () => {
    console.log('ID of el:',radio.id, 'checked state:',radio.checked); //at this point you are able to detect what is clicked, you can tell now if (ID=="" && checked==true) { run my function }. Note that checked will be true here.
  }, false);
});
<input style="margin-left:20px;" type="radio" id="orang" class="dua" name="ORDER_TYPE" value="NEW">
<label for="orang">New Registration</label>

<input type="radio" id="kucinga" class="dua" name="ORDER_TYPE" value="UPGRADE/MIGRATE">
<label for="kucinga">Upgrade/Migrate Existing</label>

<input style="margin-left:10px;" type="radio" id="kucingb" name="ORDER_TYPE" value="VAS" class="dua">
<label for="kucingb">VAS</label>
NightKn8
  • 379
  • 6
  • 18