Search code examples
jqueryclassnavigationjquery-selectors

Navigate Next or Previous div by class in jQuery


I am trying to create Navigation (Next and Previous) buttons, which displays the Next (or Previous) div having class marked.

JSFiddle Link

HTML part

<table>
<tr id="row_question_container">
<td valign="top" colspan="2">
<div id="at_test_area-1" class="at_test_area">
    <div id="at_questions_container">
        <div id="1" class="question_block unmarked" > 
            Hello world 1
        </div>
        <div id="2" class="question_block marked" style="display: none;">
            Hello world 2
        </div>
        <div id="3" class="question_block unmarked" style="display: none">
            Hello world 3
        </div>
        <div id="4" class="question_block marked" style="display: none">
            Hello world 4
        </div>
        <div id="5" class="question_block unmarked" style="display: none">
            Hello world 5
        </div>
        <div id="6" class="question_block marked" style="display: none">
            Hello world 6
        </div>
        <div id="7" class="question_block marked" style="display: none">
            Hello world 7
        </div>
        <div id="7" class="question_block unmarked" style="display: none">
            Hello world 8
        </div>
    </div>
</div>
</td>
</tr>
</table>
<input type="button" id="previous" value="Previous">
<input type="button" id="next" value="Next">

jQuery Part

$(document).ready(
function () {
    var current_question_number = 0;

    $('#next').click(function () {
        ShowMarkedQuestion("next");
    });

    $('#previous').click(function () {
        ShowMarkedQuestion("previous");
    });

    function ShowMarkedQuestion(mode) {
        var id = $(".question_block").filter(function () {
            if ($(this).css('display') == 'block') {
                return true;
            }
        }).attr('id');
        $('#' + id).hide();
        if (mode === "next") {
            current_question_number = parseInt(id) + 1;
        } else if (mode === "previous") {
            current_question_number = parseInt(id) - 1;
        } else {
            current_question_number = parseInt(id);
        }
        $('#' + current_question_number).show();
    }
});

I learnt that, For getting next div having class marked, I need to use either find() or children() like -

var marked_question = $('#at_questions_container').find('.marked').attr('id');
console.log(marked_question);

But, since 5 days I am unable to find a way to implement it in the navigation button. I mean find() can find first div with class marked, then how to navigate to next similar div. IF for finding next div I use

$('#at_questions_container').find('.marked').next('.marked').attr('id')

Then, how to get next to next having class marked?


Solution

  • you can do this by keeping track of the current id and looking for the next class that has marked. i have updated your fiddle http://jsfiddle.net/ppt4w78y/1/

    $(document).ready(
    function () {
        var self = this;
        this.currentId = '1';
        this.showNext;
    
        $('#next').click(function () {
            ShowMarkedQuestion("next");
        });
    
        $('#previous').click(function () {
            ShowMarkedQuestion("previous");
        });
    
        function ShowMarkedQuestion(mode) {
            self.showNext = false;
            var sel = $('.question_block');
            if (mode === 'previous') {
                sel = $(sel.get().reverse());
            }
    
            sel.each(function(idx, obj) {
    
                if ($(obj).attr('id') === self.currentId || !self.currentId) {
                    self.showNext = true;
                }
                else if (self.showNext && $(obj).hasClass('marked')) {
                    self.showNext = false;
                    $('#' + self.currentId).hide();
                    self.currentId = $(obj).attr('id');
                    $(obj).show();
                    return false;
                }
            });
        }
    });