0
<img src='plus.jpg' width='80' height='80' onclick="myFunction2()">

return

function myFunction2() {
        var x = document.createElement("INPUT");
        x.setAttribute("name", "image[]");
        x.setAttribute("type", "file");
        document.getElementById("add").appendChild(x);
}

I would like to use myFunction2() just 4 times. After that, I want to stop this function. Can you give me any suggestion? Thanks in advance.

4
  • what about using a looping statement? Commented Feb 28, 2016 at 11:03
  • I did but I don't know exactly where to put the index number... @RajaprabhuAravindasamy Commented Feb 28, 2016 at 11:06
  • developer.mozilla.org/en/docs/Web/JavaScript/Reference/… Commented Feb 28, 2016 at 11:07
  • Thank you!! @RajaprabhuAravindasamy Commented Feb 28, 2016 at 11:15

3 Answers 3

1

Try this :

var limit = 1;
function myFunction2() {
  if(limit <= 4){
   var x = document.createElement("INPUT");
   x.setAttribute("name", "image[]");
   x.setAttribute("type", "file");
   document.getElementById("add").appendChild(x);
   limit += 1;
  }else{
   alert("Stop");
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot @MohammaeAbed
1

solution using jQuery.

var timesClicked = 0;
$( "#imgId" ).bind( "click", function( event ) {

  // do your operations here

  timesClicked++;
  if ( timesClicked >= 4 ) {
    $( this ).unbind( event );
  }
});

Comments

0

why dont you use a counter to check when the clicks reach 4, like this

HTML:

<img src='plus.jpg' width='80' height='80' onclick="myFunction2()">

INTO JS

    var myCounter=0;  
    function myFunction2() {
if(myCounter <4){myCounter++;
            var x = document.createElement("INPUT");
            x.setAttribute("name", "image[]");
            x.setAttribute("type", "file");
            document.getElementById("add").appendChild(x);
}
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.