0

I am struggling with this thing for the last few days and trying to obtain specific div details. It was resolved actually and here is the working one - Obtain Related Div Text with jQuery. So if you have seen it, I was using individual buttons for each div section as follows:

<div class="divs">
<div class="heading">
<div class="h2Val"> //Trying to retrieve this value - The id
 1
</div>
  <div>What's the capital of England?</div>
  <div class="heading2">
    <div> 
    <input type="checkbox" id="val1" class="cbCheck" name="val1" value="London" />London</div>
    //Another one is this - CheckBox value
  </div>
  <div class="heading2">
    <div><input type="checkbox" id="val2" class="cbCheck" name="val2" value="New York" />New York</div>
  </div>
  <div>
   <input type="button" class="btn" value="Get Value" /> //This is the button that was assigned with every div
  </div>
</div>
</div>

But now, I am trying to separate the buttons and used anchor tag that's out of the parent div class divs to retrieve those values . But the issue is, it doesn't get the id and values of individual divs. Here is the code snippet:

$(document).ready(function() {
  divs = $(".divs").children();
  divs.each(function(e) {
    if (e != 0)
      $(this).hide();
  });

  var index = 0,
    divs = $(".divs").children();
  $("#next").click(function() {
    index = (index + 1) % divs.length;
    divs.eq(index).show().siblings().hide();
  })
  $("#prev").click(function() {
    index = (index - 1) % divs.length;
    divs.eq(index).show().siblings().hide();
  })

  $(".button").click(function() { //This doesn't get the id and value of heading div
    var $container = $(this).closest('.heading')
    var id = $container.find(".h2Val").text().trim();
    var $checked = $container.find('.cbCheck:checked');
    var values = $checked.map(function(){
        return this.value
    }).get();
    console.clear()
    console.log('ID: ' + id +' has ' + $checked.length + ' checked');
    console.log('Values: ', values.join())
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="divs">
  <div class="heading">
    <div class="h2Val">
      1
    </div>
    <div>What's the capital of England?</div>
    <div class="heading2">
      <div>
        <input type="checkbox" id="val1" class="cbCheck" name="val1" value="London" />London</div>
    </div>
    <div class="heading2">
      <div><input type="checkbox" id="val2" class="cbCheck" name="val2" value="New York" />New York</div>
    </div>
  </div>

  <div class="heading">
    <div class="h2Val">
      2
    </div>
    <div>Who invented computer?</div>
    <div class="heading2">
      <div><input type="checkbox" id="val3" class="cbCheck" name="val3" value="Thomas Edison" />Thomas Edison</div>
    </div>
    <div class="heading2">
      <div><input type="checkbox" id="val4" class="cbCheck" name="val4" value="Charles Babbage" />Charles Babbage</div>
    </div>
    <div class="heading2">
      <div><input type="checkbox" id="val5" class="cbCheck" name="val5" value="Sir Isaac Newton" />Sir Isaac Newton</div>
    </div>
  </div>
</div>

<a class="button" id="prev">Previous</a>
<a class="button" id="next">Next</a>

So I tweaked a bit with the following but it retrieves all the selected div values at a time rather than individual:

 $(".button").click(function() { //This doesn't get the id and value of heading div
        var $container = $('.divs').children().closest('.heading')
        var id = $container.find(".h2Val").text().trim();
        var $checked = $container.find('.cbCheck:checked');
        var values = $checked.map(function(){
            return this.value
        }).get();
        console.clear()
        console.log('ID: ' + id +' has ' + $checked.length + ' checked');
        console.log('Values: ', values.join())
      });
    });

Any better way or solution to resolve it?

Update 1: Expected output - When checked on first option and clicked Next, it should show result as follows:

ID: 1  has 1 checked
Values: London

Then when second question comes and the same should happen:

ID: 2  has 1 checked
Values: Charles Babbage
AT-2017
  • 3,114
  • 3
  • 23
  • 39
  • `$('.divs').children()` OR `$('.divs').find('.heading')` instead of `$('.divs').children().closest('.heading')` .. So try `$('.divs').find('.heading').eq(index);` and see if this is what you want – Mohamed-Yousef Jul 16 '20 at 07:41
  • Close enough @Mohamed-Yousef and thanks a lot for it. Could you please see my updated post? I've given a sample how the output should appear. – AT-2017 Jul 16 '20 at 08:31

1 Answers1

1

You can get length of .divs and then declare some variable which will have value 0 .So, whenever next button is clicked you can check if the variable value if less then the length of .divs and depending on this either increment value by 1 or start counter again from 0.

Demo Code :

$(document).ready(function() {
  divs = $(".divs").children();
  divs.each(function(e) {
    if (e != 0)
      $(this).hide();
  });

  var index = 0,
    divs = $(".divs").children();
  $("#next").click(function() {
    index = (index + 1) % divs.length;
    divs.eq(index).show().siblings().hide();
  })
  $("#prev").click(function() {
    index = (index - 1) % divs.length;
    divs.eq(index).show().siblings().hide();
  })
  //declare
  var indexes = 0;
  $(".button").click(function() { 
  //get div length
    var lengths = divs.length;
    //pass indexes value to get required div
    var $container = $('.divs').children().eq(indexes);
   
    var id = $container.find(".h2Val").text().trim();
    var $checked = $container.find('.cbCheck:checked');
    var values = $checked.map(function() {
      return this.value
    }).get();
    console.clear()
    console.log('ID: ' + id + ' has ' + $checked.length + ' checked');
    console.log('Values: ', values.join())
    //checking if indexes value is less then length of div
    if (indexes < (lengths-1)) {
    //increment
      indexes++;
    } else {
    //start from 0 
      indexes = 0;
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="divs" datas="abc">
  <div class="heading">
    <div class="h2Val">
      1
    </div>
    <div>What's the capital of England?</div>
    <div class="heading2">
      <div>
        <input type="checkbox" id="val1" class="cbCheck" name="val1" value="London" />London</div>
    </div>
    <div class="heading2">
      <div><input type="checkbox" id="val2" class="cbCheck" name="val2" value="New York" />New York</div>
    </div>
  </div>

  <div class="heading">
    <div class="h2Val">
      2
    </div>
    <div>Who invented computer?</div>
    <div class="heading2">
      <div><input type="checkbox" id="val3" class="cbCheck" name="val3" value="Thomas Edison" />Thomas Edison</div>
    </div>
    <div class="heading2">
      <div><input type="checkbox" id="val4" class="cbCheck" name="val4" value="Charles Babbage" />Charles Babbage</div>
    </div>
    <div class="heading2">
      <div><input type="checkbox" id="val5" class="cbCheck" name="val5" value="Sir Isaac Newton" />Sir Isaac Newton</div>
    </div>
  </div>
</div>

<a class="button" id="prev">Previous</a>
<a class="button" id="next">Next</a>
Swati
  • 28,069
  • 4
  • 21
  • 41
  • It worked like a charm @Swati. Thanks a lot, can you do one favor if you have time? In the **Previous** and **Next** click event, I am trying to block the events if it reaches the last div. Say the second question is reached and user clicks on **Next** event, it shouldn't go forward but it can go to the previous question. In the same way, **Previous** event should do the same. I tried with this statement but it didn't get me the result as expected - `if(divs.eq(index).next().length != 0)` for next event. – AT-2017 Jul 16 '20 at 14:06
  • You mean you need to disable the next button if the last question is reached and same for previous? – Swati Jul 16 '20 at 16:25
  • does these data are dynamic ? or static ? Also on click of previous button you need to show that result or not ? – Swati Jul 17 '20 at 05:01
  • The data showing are dynamic. For testing purpose, I showed static data at the moment. Yes, when user clicks on the events, it should display respective div data like you have done in the sample code snippet of yours. – AT-2017 Jul 17 '20 at 07:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218020/discussion-between-swati-and-at-2017). – Swati Jul 17 '20 at 09:28
  • @AT-2017 check this [fiddle](https://jsfiddle.net/Swati911/mLezntd2/11/) if that works for you. – Swati Jul 18 '20 at 07:58
  • This worked perfectly except the last one I meant the last question. Rather than that perfect solution. I can handle that in another way but will try to do it as the **Previous** event. Thanks a lot for your time and effort. – AT-2017 Jul 18 '20 at 10:54
  • If you have time, check on this @Swati - https://stackoverflow.com/questions/63103847/get-specific-div-content-based-on-id. Thanks. – AT-2017 Jul 26 '20 at 18:07