开发者

Jquery find next index

开发者 https://www.devze.com 2023-02-04 00:16 出处:网络
What is the correct way to find the index of elements.I am using a div that appends a new div into a container and I must \"have\" a new ID based on the index - this allows for easy indexing on a dele

What is the correct way to find the index of elements. I am using a div that appends a new div into a container and I must "have" a new ID based on the index - this allows for easy indexing on a delete function later. i.e. I can just run a each 开发者_Go百科function to update the id's

this is my code: - I want to find the index of class .pollq when #addoption is clicked

$('#addoption').live('click',function(){
    var index = index('.pollq');

    alert(index);   

    $(this).parent('form').append('<br /><label for="">Option #'+($('.pollq').length+1)+'</label><input type="text" id="polloption'+($('.pollq').length+1)+'" class="pollq generate" />');

    $('#pollpreview').append('<br /><input type="radio"><span class="polloption'+($('.pollq').length)+'">Options polloption'+($('.pollq').length)+'</span>');

});


$('#addoption').live('click',function(){
    var i = $(this).parent('form').find('.pollq').length;
    var id = "polloption"+i;

    $(this).parent('form').append(
        '<br /><label for="'+id+'">Option #'+i+'</label><input type="text" id="'+id+'" class="pollq generate" />'
    );

    // note that this should be a <label>, too
    $('#pollpreview').append(
        '<br /><input type="radio"><span class="'+id+'">Options '+id+'</span>
    ');
});


This will give you the index of an element with the class "pollq", relative to its container:

var index = $('.pollq').index();

Is that what you wanted?

i.e. index would be "2" in the following example:

<div>
    <div class="something"></div>
    <div class="something-else"></div>
    <div class="pollq"></div>
</div>
0

精彩评论

暂无评论...
验证码 换一张
取 消