cve60069 Posted June 13, 2013 Share Posted June 13, 2013 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 More sharing options...
dsonesuk Posted June 13, 2013 Share Posted June 13, 2013 (edited) 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 June 13, 2013 by dsonesuk Link to comment Share on other sites More sharing options...
cve60069 Posted June 14, 2013 Author Share Posted June 14, 2013 I have amended my code but I still get 0. I have changed the code so that the modal-line is selected. The remove button stores the index. Thanks for your help. paragraph v2.html Link to comment Share on other sites More sharing options...
dsonesuk Posted June 14, 2013 Share Posted June 14, 2013 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 More sharing options...
cve60069 Posted June 14, 2013 Author Share Posted June 14, 2013 Many thanks. You are a great help. Link to comment Share on other sites More sharing options...
dsonesuk Posted June 15, 2013 Share Posted June 15, 2013 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now