Jump to content

Index of a clone


cve60069

Recommended Posts

Hello I am working on some javascript routines that will allow me to manipulate local storage. I have created a page that may be edited through a modal-dialogue. I can add a line to the page via the modal but now I want to select a line and work on it (edit, delete, move). The last line of my code '$("div.fItemClass").click(function(event){alert($(this).index())}' is supposed to give me the index of any of the div's I clone but I always get '0'. Any clues please? Regards

paragraph v2.html

Link to comment
Share on other sites

If you are cloning, AFTER page load, 'click' event should not work as it applys click event to target elements that are rendered and found on loading of the page, elements created/cloned after page load should use (depending on version of jquery) .live(), .delegate(), or for newest version .on()

$(function(){$('body').on('click', '.fItemClass', function(event){var index = $(".fItemClass").index(this)alert(index)  }); });

Edited by dsonesuk
Link to comment
Share on other sites

I think you find 'this' is picking up the click event of button, so within its container its index value will be 0 the button can be targetted to retrieve a index value, although it is correct its is not targetting '.mTextClass'

$('#modalContainerId').on('click', '.removeable', function(event){var cIndex = $(".removeable").index(this);alert(cIndex)})

if you wish to target '.mTextClass' you would probably have move focus to parent() of button, then! the sibling.

Link to comment
Share on other sites

Ithink you will find you are duplicating id references when they should be unique within a page

    //$('.mItemClass').append($('#modalContainerId').clone().html(mHTML)) should be    $('.mItemClass').clone().appendTo($('#modalContainerId').html(mHTML))

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...