Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it correct to put an event inside an event?

I have an script in which I'm going to add a file XLS, once that I validate the file format, I close a bootstrap's modal and open another modal which is an confirmation window to see whether the user is sure to upload that file.

This confirmation window has a confirmation button, once clicked I want that execute me an function which it's going to run an AJAX to make the request to the server.

However, because of that, I had the following doubts:

  • Which of the 2 ways is better (and the most correct) to run the code and why?
  • Why is the click event of the first input file executed if there has not been an event change? I mean, I add a file and the event change is executed and I can make clicks many times as I want, is not it supposed that I must add another file so that I can run the function inside again?
  • Put an event inside an event, has it a name?

$(document).ready(function(){
  //First input file
  $(document).on('change','#file', function(){
    let file = $(this);
    let nameFile = file[0].files[0].name;
    let button = '<button type="button">Clic input 1</button>';
    
    $('#button').html(button);
    
    $('#button').click(function(){
      console.log('CLICK IN FIRST INPUT FILE!');
    });
    
  });
  
  //Second input file
  $(document).on('change','#file2', function(){
    let file = $(this);
    let nameFile = file[0].files[0].name;
    let button = '<button type="button">Clic input 2</button>';
    
    $('#button2').html(button);
  });
  
  $('#button2').click(function(){
      console.log('CLICK IN SECOND INPUT FILE!');
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="file" name="file" />
<div id="button"></div>

<div style="margin-top:20px"></div>

<input type="file" id="file2" name="file2"/>
<div id="button2"></div>
like image 324
Hoose Avatar asked Feb 02 '18 14:02

Hoose


2 Answers

Put an event inside an event, has it a name?

It has, the name is Bad Idea. Let me Expain. What happens when you execute the following code.

$('#button').click(function(){
  console.log('CLICK IN FIRST INPUT FILE!');
});

A click event is registered to the button. Once an event is registered, it will fire everytime no matter how many times you click.

When you put that code inside another event handler like the first example, it gets executed everytime the file-input changes and a new event handler is registered. So when you select a file, and then decide to change it, file-input changes twice and you get 2 click events registered. Now click on the button, you get 2 new console log printed by one click!!! Try it..

Why is the click event of the first input file executed if there has not been an event change

Because that's how event handler works, you register once, they get fired everytime after that.

Which of the 2 ways is better (and the most correct) to run the code and why?

Obviously not the first one, because it is a bad idea, Not the second one either. In case of second one you are attaching event to a division that will contain the button. So you don't need to click on the button, just click anywhere right side of the button, the event gets fired!!!

So if none of them is right, what can we do?

  • Do not generate button/any html element by javascript for such simple tasks. Do it with HTML, plain and simple.
  • Do not nest event handler into another i.e put one event handler inside another, it will complicate things. Just put all event handlers directly inside document.ready event of jQuery. document.ready only fires once.
  • When you need to control user action then show/hide your button or other html element by javascript based on required conditions.

My suggestion is doing something like this.

$(document).ready(function(){
    // Hide the button at first
    $('#button').hide();
    // When File-input changes
    $('#file').change(function(){
        if(**the file-input has a file selected**){
            $('#button').show();
        }
        else{
            $('#button').hide();
        }
    });
    // When Button Clicked
    $('#button').click(function(){
        // Do the action
    });
});
like image 123
Munim Munna Avatar answered Sep 23 '22 15:09

Munim Munna


Which of the 2 ways is better (and the most correct) to run the code and why?

I believe this is better:

//Second input file
$(document).on('change','#file2', function(){
  let file = $(this);
  let nameFile = file[0].files[0].name;
  let button = '<button type="button">Clic input 2</button>';

  $('#button2').html(button);
});
$('#button2').click(function(){
  console.log('CLICK IN SECOND INPUT FILE!');
});

Mainly because it's more readable and easy to follow. There is no need to have the button click event set up AFTER the input has been changed. It is better to change the STATE of the button, as you are doing. Even better would be to hide/show the button like:

$('#button2').show();

And have it initially hidden with:

<div id="button2" style="display: none">Click me</div>

Why is the click event of the first input file executed if there has not been an event change?

In my test, this all worked correctly.

How is called this?

The change events should only be called when you click and assign a file to the input.

like image 34
benjaminhull Avatar answered Sep 23 '22 15:09

benjaminhull