Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select the next element with same class?

My question is formed by two parts:

  1. How to select the next element with the same class and hide the previous one?
  2. How can I add in each input hidden value, the value of button that is pressed? Let's say I press the button with class 'no'. I want the 'no' to go in it's input hidden value of the question. How to do that?

Thank you so much. Any kind of help would be kindly appreciated.

This is what I have so far:

HTML:

$('.answer_buttons a').on('click', function(e){
    e.preventDefault();
    $('.selected').removeClass('selected').hide().next().show().addClass('selected');
    $.ajax({
        type: "POST",
        url: '',
        data: {},
        success: function(msj){
            //alert('test');
            //console.log(msj);
        }
    });
});
<form action="" method="post">
  <input name="intr1" value="" type="hidden">
  <p class="question selected" id="intreb_1">
    Intrebarea 1?</p>
  <input name="intr2" value="" type="hidden">
  <p class="question " id="intreb_2">
    Intrebarea 2?</p>
  <input name="intr3" value="" type="hidden">
  <p class="question " id="intreb_3">
    Intrebarea 3?</p>
  <input name="intr4" value="" type="hidden">
  <p class="question " id="intreb_4">
    Intrebarea 4?</p>
  <input name="intr5" value="" type="hidden">
  <p class="question " id="intreb_5">
    Intrebarea 5?</p>
  <div class="answer_buttons">
    <a class="nu" href=""></a>
    <a class="da" href=""></a>
  </div>
</form>
like image 509
Ionut Avatar asked Sep 23 '15 09:09

Ionut


1 Answers

It seems you want to select the next sibling .question of the current .question.selected element. For doing this you can use the .nextAll() and .first() methods:

$('.selected').removeClass('selected')
              .hide()
              .nextAll('.question')
              .first()
              .show()
              .addClass('selected');

And for updating value of the previous input sibling by using the className of the clicked element you can use the .prev() and .val() methods.

$('.selected')...
              .addClass('selected')
              .prev('input')
              .val($(this).hasClass('nu') ? 'no' : 'yes');
like image 61
undefined Avatar answered Oct 07 '22 07:10

undefined