lundi 20 juin 2016

Getting a specific id using jquery

I've a code which is like that :

<div class="answer_id" value="1" name="false">
  <input id="AsnwerAnswer1" type="checkbox" value="1" name="true"></input>
  Answer 1
</div>

I've 4 answers that are like the code above, the only thing that change is the value in my div and the input id's.

When I click on a checkbox the selected checkbox get the value true :

$("input:checkbox").click(function() {
     var thisChecked = $(this)[0].checked;
        $("input:checkbox").removeAttr('checked');
        $("input:checkbox").attr('name', 'false');
        $(this).attr('name', 'true');
        $(this)[0].checked = thisChecked ? true : false;
 });

When I click on the button to send the answer :

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

I would like to get the value of the div that contains the selected checkbox (the one who has its value at true).

What should I add to this :

var iReponseId = $(this).closest('.answer_id').attr('value');

By the way it would be really great if you could help me understand what the click function does. According to me, this remove all the atrributes checked so I can only get one checkbox selected, this set the name to every checkbox to false expect for the one that is selected but I don't understand this :

 $(this)[0].checked = thisChecked ? true : false; 




Aucun commentaire:

Enregistrer un commentaire