1

I have step form with 3 steps. I want to get index of <li> in stepinstallment when <label> inside it selected to check:

  1. If the first step is selected, it will change background color of circle class.

  2. If the second and third step is selected without first step, it will display error message.

I've tried many ways on SO and write myself but it doesn't work.

This is my code

<ul class="clearfix">
    <li>
        <div class="step-one circle">
            <p>Step 1</p>
        </div>
    </li>
    <li>
        <div class="step-two circle">
            <p>Step 2</p>
        </div>
    </li>
    <li>
        <div class="step-two circle">
            <p>Step 3</p>
        </div>
    </li>
</ul>

<div class="stepinstallment">
    <ul>
        <li id="step-one" class="step">
            <h2>Choose your document</h2>
            <div class="step-content">
                <label>
                    <input type="radio" id="option-1" name="options" value="1" />
                    <span>ID card and household registration</span>
                </label>
            </div>
        </li>

        <li id="step-two" class="step">
            <h2>Choose your document</h2>
            <div class="step-content">
                <label>
                    <input type="radio" id="option-1" name="options" value="1" />
                    <span>ID card and household registration</span>
                </label>
            </div>
        </li>

        <li id="step-three" class="step">
            <h2>Choose your document</h2>
            <div class="step-content">
                <label>
                    <input type="radio" id="option-1" name="options" value="1" />
                    <span>ID card and household registration</span>
                </label>
            </div>
        </li>
    </ul>
</div>

JS

var sCircle = $('.steps ul li');

    $('.step label').on('click', function(){
        var $this = $(this),
            sCircleIndex = parseInt(sCircle.index()),
            sCircleChild = sCircle.children('div'),
            currParent = $this.parents('ul').index();

        console.log(currParent);
    });

Above JS code always return 0. Hope anyone can figure me out this case. Thanks in advance.

The Hung
  • 309
  • 4
  • 18
  • Possible duplicate of [Get index of clicked element in collection with jQuery](http://stackoverflow.com/questions/5545283/get-index-of-clicked-element-in-collection-with-jquery) – Itay Oct 22 '15 at 05:53
  • `$('ul').index($this.parents('ul'));` – adeneo Oct 22 '15 at 05:54

7 Answers7

2

Try using .closest()

$(".stepinstallment label").on("click", function() {
    console.log($(this).closest("li").index())
});

    $('.stepinstallment label').on('click', function(){
        console.log($(this).closest("li").index())
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul class="clearfix">
    <li>
        <div class="step-one circle">
            <p>Step 1</p>
        </div>
    </li>
    <li>
        <div class="step-two circle">
            <p>Step 2</p>
        </div>
    </li>
    <li>
        <div class="step-two circle">
            <p>Step 3</p>
        </div>
    </li>
</ul>

<div class="stepinstallment">
    <ul>
        <li id="step-one">
            <h2>Choose your document</h2>
            <div class="step-content">
                <label>
                    <input type="radio" id="option-1" name="options" value="1" />
                    <span>ID card and household registration</span>
                </label>
            </div>
        </li>

        <li id="step-two">
            <h2>Choose your document</h2>
            <div class="step-content">
                <label>
                    <input type="radio" id="option-1" name="options" value="1" />
                    <span>ID card and household registration</span>
                </label>
            </div>
        </li>

        <li id="step-three">
            <h2>Choose your document</h2>
            <div class="step-content">
                <label>
                    <input type="radio" id="option-1" name="options" value="1" />
                    <span>ID card and household registration</span>
                </label>
            </div>
        </li>
    </ul>
</div>
guest271314
  • 1
  • 15
  • 104
  • 177
  • Thank you. This code works for me. I want to check if index >= 1, it will check prev step circle, if it doesn't have class active. It will display error message. Could you help me please? – The Hung Oct 22 '15 at 06:20
  • @TheHung `if ($(this).closest("li").index() >= 1) {//do stuff}` – guest271314 Oct 22 '15 at 06:23
  • Yes, thank you. I know this stuff. I mean is if `index` >= 1, I want to get list `
  • of steps circle. `if ($(this).closest("li").index() >= 1) { // check prev steps circle, if prev step have active class, it will add class for current step }`
  • – The Hung Oct 22 '15 at 06:27
  • 1
    @TheHung Try `if ($(this).closest("li").index() >= 1) {if ($(this).closest("li").prev(".step").is(".active")) {$(this).closest("li").addClass("active")}}` – guest271314 Oct 22 '15 at 06:34