0

i have this following script :

$(function() {
    var no_of_file = {{$image_counter}};

            $('#remove-file').click(function() {
                 no_of_file--;
            }); 

    if (no_of_file >= '5'){
        $("#dv1").hide();
        $("#imageLabel").hide();
    }else{              
        /* Append More Input Files */
        $('#anc_add_more').click(function() {
            no_of_file++;
            // alert(no_of_file);
            if(no_of_file < {{$role_pic_limit}})
            {
            $('#spn_inputs').append('<span><input type="file" name="myfile[] id="fileChooser" style="float:right;"/><a href="javascript:void(0);"  onclick="$(this).parent().remove();" style="float:left;" id="remove-file" >Remove</a><br/></span>');      

            }
            else{ 
                alert("You can upload only {{$role_pic_limit}} images");
            }
        }); 

    }
});

When i click "Add more file" i append more input and add +1 to no_of_file,my problem is when i try press Remove , it's should down -1 to no_of_file var , why it's not down increment ?

EDIT 15/02:

$(function() {
    var no_of_file = {{$image_counter}};

    if (no_of_file >= '5'){
        $("#dv1").hide();
        $("#imageLabel").hide();
    }else{              
        /* Append More Input Files */
        $('#anc_add_more').click(function() {
            no_of_file++;

            // alert(no_of_file);
            if(no_of_file < {{$role_pic_limit}})
            {
            $('#spn_inputs').append('<span><input type="file" name="myfile[]" class="fileChooser" style="float:right;"/><a href="javascript:void(0);"  onclick="$(this).parent().remove();" style="float:left;" id="remove-file" >Remove</a><br/></span>');      

            }
            else{ 
                alert("You can upload only {{$role_pic_limit}} images");
            }
        }); 

       $(document).on('click', '#remove-file,.anc_delete_more', function() { 
           no_of_file--;
        });

    }
});

This is now working , but decrease is not perfect is like that :

Click add more file +1
Click add more file +1
Click add more file +1
Click remove more file -1
Click remove more file nothing no decrease
Click remove more file nothing no decrease

jsfiddle:

https://jsfiddle.net/dgh3ndpm/2/

please view jsfiddle i have uploaded what i am using , and you can see clearly the issue , press add more 5 times , then press remove and then press add more again. thanks a lot

You can see how it's work not that good why ?

Raviv g
  • 29
  • 5
  • It looks like it should you just aren't then removing the actual entry. We need to see your HTML to help you further though. – Sumner Evans Feb 15 '15 at 00:59
  • Ids have to be **unique** - And why you're using inline scripts in the file inputs instead of jQuery? – Andreas Feb 15 '15 at 01:01
  • this script needs to be in a function and you need to run that function evey time you increase or decreae the value – Miro Feb 15 '15 at 01:04
  • Ok i will try some thing with function for inc or dec . – Raviv g Feb 15 '15 at 01:13
  • @miro The increase and decrease operations are included in a function - the click event handlers of `#anc_add_more` and `#remove-file` (the dublicated id! Use a class instead) – Andreas Feb 15 '15 at 01:22
  • @Andreas no! they are not. They are all in a function, but that fuction doesn't get called after a click. It doesn't get refreshed... – Miro Feb 15 '15 at 02:20
  • @miro You're right but so am I^^ The incrementation is part of the `$('#anc_add_more').click(...)` event handler. But the decrementation part is in a function that isn't called by the dynamically added inputs - that's what I've missed... :| – Andreas Feb 15 '15 at 02:35
  • Thanks , i got the hints , anyway i success to make some thing to work with 'on' event , anyway this is not working perfect please view my issue i edited with JSFIDDLE. – Raviv g Feb 15 '15 at 09:10

2 Answers2

1

Use numbers, not string: (javascript automatically select what he thinks the varible is, it often confuse string instead of a number, divide by 1 guarantee we will have an 'int' varible... I changed it in your fiddle, and it works great...

edit: i just see, it only works for the first 'remove', so you clearly have an issue with the ID.

Use class selector instead.

edit: OK, some issues, but this is 100% working!! :P

var no_of_file = 0 / 1;
$(function () {
no_of_file = 0 / 1;
if (no_of_file >= 5) {
    $("#dv1").hide();
    $("#imageLabel").hide();
} else {
    /* Append More Input Files */
    $('.anc_add_more').click(function () {
        alert(no_of_file);
        if (no_of_file < 5) {
              no_of_file++;
            $('#spn_inputs').append('<span><input type="file" name="myfile[]" class="fileChooser" style="float:right;"/><a href="javascript:void(0);" style="float:left;" class="remove-file" >Remove</a><br/></span>');

        } else {
            alert("You can upload only 5 images");
        }
        initRemove();
    }

    );
  }
}

);

function initRemove() {
$(".remove-file").unbind('click');
$('.remove-file').click(function () {
    $(this).parent().remove();

    no_of_file--;
});
}
Ziv Weissman
  • 4,400
  • 3
  • 28
  • 61
  • I used you'r hints , and also changed in jsfiddle but its still the same not working good.. are you sure you updated my jsfiddle , because i saw the same and i changed my self to test not working. – Raviv g Feb 15 '15 at 09:33
  • k, sry didn't check all. Now this is working. Explain: 1. The varible should be outside the pageload function so it will be general varible and not only for the pageload function, 2. The event needs to be initiated on every Add of 'remove' span. 3. Unbind all previous events so they will not occur more than once per click. – Ziv Weissman Feb 15 '15 at 09:49
  • Sory one more issue, add files untill limit 5 and then remove you can see its will do the same issue as before. – Raviv g Feb 15 '15 at 11:23
  • yeah the increase should be only if the limit is not reached and not every click... working fiddle - https://jsfiddle.net/oud79k7p/ – Ziv Weissman Feb 15 '15 at 11:32
  • I have one more issue with the same can you chat with me ? – Raviv g Feb 15 '15 at 13:00
  • Can you chat via Stackoverflow? – Ziv Weissman Feb 15 '15 at 13:59
0

EDIT 15/02:

http://jsfiddle.net/ccn35w40/6/

I was using classes instead of IDs and was able to attach event to tags which ware added latter to DOM with these:

$(document).on('click', '.remove-file', function () {
  alert('remove one #files:'+no_of_file);

  $(this).parent().remove();
  no_of_file--;
});

Original post:

With the rest of HTML and working jsfiddle would be easier to debug. This will not be as answer as per say, more what steps you could do when you get issues with code like this.

var no_of_file = 2;

$('#remove-file').click(function() {
   no_of_file--;
   alert(no_of_file);
}); 

$('#add-file').click(function() {
  no_of_file++;
  alert(no_of_file);
}); 

http://jsfiddle.net/ccn35w40/1/

But it can work, add alert(); and console.log(); to see if the event is not triggered at all or it is triggered but the code doesn't do what you want. From that point you will know more which way to go when debugging it. The incrementing & decrementing by itself works, so the issue related to the rest of js.

You seam to do few things wrong, you try having onClick and jQuery event handler attached to it. In reality the jQuery will not be attached because it was called much sooner than the HTML tag poped into existence. And you have duplicate IDs. Decrements is not issue, it's not just called at all. The jQuery events are attached just to element that are existing at the moment of execution. If you will add another one later the event will not be added to it. To get rid of duplicate id you can use class instead of id and then use $('.remove-file').click()

Notice how the # changed to dot. The hashtag is for IDs and dot for classes.

Instead then you can do these don't have to be unique.

You can try using .live so the event listener will be added even for your fresh tags.

jQuery function not binding to newly added dom elements

Community
  • 1
  • 1
Anton Krug
  • 1,555
  • 2
  • 19
  • 32
  • The link you attached is working , but not perfect , when i add +1 counter ,it working perfect , but when -1 not good ,i have updated my code + jsfiddle. – Raviv g Feb 15 '15 at 08:55
  • Sorry for double post , please view jsfiddle i have uploaded what i am using , and you can see clearly the issue , press add more 5 times , then press remove and then press add more again. thanks a lot . – Raviv g Feb 15 '15 at 09:07
  • Strange the link for me is working. I think I explained why you think the decrement is not working. And how to fix it. Classes instead of ids and **live** instead of regular even listener – Anton Krug Feb 15 '15 at 10:30
  • I just realized live is deprecated. on is now used.... This fiddle works without issues: http://jsfiddle.net/ccn35w40/6/ Updated my answer – Anton Krug Feb 15 '15 at 14:59
  • I think my approach is cleaner than voted answer where you are binding and unbiding events every time. You don't have to change vote. Just have a look at the fiddle if you would like it – Anton Krug Feb 16 '15 at 14:06