There is html:
<form method="POST" enctype="multipart/form-data" id="main-image-form"> <input name="image_url"> <button type="submit" enctype="multipart/form-data" class="save btn btn-warning"> <b>-100 G</b> <img src="reload.png'"> </button> <div name="main" onclick="backgroundPreview(this);"></div> <button name="btn"></button> </form>
And JS:
jQuery(document).ready(function ($) { $('#main-image-form').submit(function(e){ // some code }); }); function backgroundPreview(elem) { console.log(elem); console.log(elem.name); }
Firstly , the first JS function works even when you click on the button , whose name="btn" . I do not understand why. Judging by the function, it should only work when you click on a submit form button.
Secondly , when you click on the div , in which name="main" , the backgroundPreview function is called, to which the element object is transferred from html. The object itself in the console is printed. But instead of its name parameter, undefined is displayed in the console. Why? After all, this div has a name attribute. If this div changed to button , then the name will be printed, but then the first JS function will be called, as described in the first paragraph, and I should not allow this.
What can I do to ensure that when I click on a div only one second JS function is invoked, and I could get the name attribute from this div from JS?