A
A
Anatoly2021-07-19 03:39:18
JavaScript
Anatoly, 2021-07-19 03:39:18

Why is another event fired?

Why, in the example, does another event fire? How to turn it off?
Open the console, stand on input and press Enter.
In the console it is not written <button>Добавить</button>, but there was no click on the "Add" button.


Example code
<form action="" onsubmit="return false">
  <table>
    <thead><tr><td>Наименование</td><td>Цена</td></tr></thead>
    <tbody>
      <tr>
        <td><input type="text"></td>
        <td><input type="text"></td>
      </tr>
    </tbody>
  </table>

  <button>Добавить</button>    
</form>

window.addEventListener('click', selected, false);
function selected(e) {
  e.preventDefault(); 
  e.stopImmediatePropagation();
  
  if ( e.target.tagName == 'BUTTON' ) console.log(e.target);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Chipekwe, 2021-07-19
@Tolly

You need to add type="button"a button.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question