fidel fidel - 6 months ago 28
HTML Question

increment class value onclick javascript

im trying to increment class value on click. how can i achieve this using this code? http://jsfiddle.net/P9C7E/29/


Generate New Div

<div class="container">
<div data-bind="foreach:items" class="fix_backround">
<div href="#" class="item" data-bind="draggable:true,droppable:true">
<span data-bind="click:$parent.remove">[x]</span><br/><br/>
<center><span class="text" data-bind="text:$data"></span><input class="edit_text"/></center>
</div>


is it even possible to increment class="item" example class="item1" item2 item 3 exc increment by one every time draggable is created

Answer

You could do something like this to add 'item1', 'item2', etc. for each draggable added.

var count = 1;

ko.bindingHandlers.draggable={
    init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
        $(element).draggable();
        $(element).addClass('item' + count);
        count++;
    }
};

(Just keep in mind that since you're already using an MV* framework, you most likely can avoid having to do things like enumerate classes)

Comments