2

My radio button are named dynamically. How to check all the checkbox on div click. The div and each radio has unique id. The div actually represent cards. I want upon clicking on card how to select all the listed radio in div How to dynamically get all child node from javascript but check all the radio

<div id=<?= ($counter-1) ?> class="card categoryCard" style="width: 18rem;">
  <div class="card-body">
    <h5 class="card-title"><?= $rows[$counter-1]["TICKETDESCRIPTION"]?></h5>
    <input type="radio" id=<?='TICKETDESCRIPTION'.($counter-1) ?> name="TICKETDESCRIPTION" >
    <p class="card-text"><label>RM <?= $rows[$counter-1]["TICKETPRICE"]?></label></p>
    <input type="radio" id=<?='TICKETPRICE'.($counter-1) ?> name="TICKETPRICE" >
  </div>
</div>

Below is my javascript code

$('.categoryCard').click(function(){
    var id = $(this).attr('id');
    var radiobtn = document.getElementById("TICKETDESCRIPTION"+id);
    radiobtn.checked = true;
    alert(id);
});
3
  • why are you mixing up jQuery with javascript ?. that is not a good practice. var radiobtn = document.getElementById("TICKETDESCRIPTION"+id); this can be replaced with var radiobtn = $("#TICKETDESCRIPTION"+id); Commented Dec 28, 2018 at 4:20
  • you can select all the radio button using 'input[type=radio]' Commented Dec 28, 2018 at 4:22
  • @AkhilAravind Yes, but lets say i have 2 radio button, it doesnt select all the radio button when i click on the div Commented Dec 28, 2018 at 4:24

3 Answers 3

1
// assuming jquery

$('.categoryCard').click(function(e){
   $('input[type="radio"]', this).each( function( i) {
     $(this).prop('checked', true);

   });

});
Sign up to request clarification or add additional context in comments.

6 Comments

can you explain what is function(i, el)?
For it to be check, should i use "attribute" instead of "property"?
@MrJ No, he is actually correct. This is because my i am looping and i need to make cards in such a way that each card has same radio button but different value.
actually it was little wrong, see update - but no - prop is the correct jquery method see here : api.jquery.com/each
Can explain why have "this" in $('input[type="radio"]', this)
|
1

There are many ways to implement the same. I've described the two ways.

You must not use the ID as duplicate.

ID is identifier for the elements, so it must not be duplicate.

$('.categoryCard').click(function(e){
   $('input[type="radio"]', this).each( function( index, element) {
     $(element).prop('checked', true);

   });

});

$('.categoryCard').click(function(e){
  var id = $(this).attr('id');
   $('input[data-details="'+ id +'"]', this).each( function( index, element) {
     $(element).prop('checked', true);

   });

});
.categoryCard{
border: 1px solid #d4d4d4;
padding: 5px;

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="1" class="card categoryCard" style="width: 18rem;">
  <div class="card-body">
    <h5 class="card-title">Desc</h5>
    <input type="radio" name="TICKETDESCRIPTION">
    <p class="card-text">Price</p>
    <input type="radio" name="TICKETPRICE" >
  </div>
</div>

<!-- 2nd Method there must be no duplicate ID  -->

<div id="2" class="card categoryCard" style="width: 18rem;">
  <div class="card-body">
    <h5 class="card-title">Desc</h5>
    <input type="radio" name="TICKETDESCRIPTION" data-details ="1">
    <p class="card-text">Price</p>
    <input type="radio" name="TICKETPRICE" data-details ="1" >
  </div>
</div>

1 Comment

this will help alot of people
1

It can be also solved like this:-

$('.categoryCard').click(function(){
   $(this).find('input[type="radio"]').each(function() {
     $(this).prop('checked', true);
   });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.