This question already has answers here:
How can I trigger a JavaScript event click
(9 answers)
Closed 2 years ago.
I want to fire an trigger click event with JavaScript. I did it with jQuery like this:
$('button[type="submit"]').trigger('click'):
I want the same with JavaScript.
You can use
document.getElementById('buttonID').click();
in vanilla JS
function callClick() {
document.querySelector('button[type="submit"]').click();
}
<button type="submit" onmouseover="callClick();" onclick="alert('Click called!')">Submit</button>
This should do the trick:
document.querySelector('button[type="submit"]').click();
you can use click . The HTMLElement.click() method simulates a mouse click on an element.When click() is used with supported elements (such as an <input>), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.
Syntax
setTimeout(function(){
let submit =document.getElementById('submit');
submit.click();
},2000)
function submited(){
alert('submitted');
}
<form onsubmit="submited()">
<input type="text">
<input type="submit" id="submit">
</form>
Related
This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 10 months ago.
I have problem with following code. I have a div with id reloadDiv that is refreshing every 3 seconds using ajax. Content of the div is loaded using php, there is more content in this div but I didn't added it here.
Div is reloading normally. The problem is with input type button event with class replyComment, there are multiple buttons with this class here. When I add click event the event is working until first reload, as soon as div reloads event is not working any more.
Does someone know how to solve this problem?
<div id="reloadDiv">
<?php foreach($comments as $c):?>
<input type="button" value="Reply" class="replyComment" />
<?php endforeach?>
</div>
js:
setInterval(function(){
$("#reloadDiv").load(location.href + " #replyComment");
}, 3000);
$('.replyComment').click(function(){
console.log("smth")
});
That happens because content is replaced, and the element with the event doesn't exists anymore, as well the event.
Try this:
$('#reloadDiv').on('click', '.replyComment', function() {
With that, the event is bound to the parent element and will work no matter how many times the content is recreated.
This question already has answers here:
mousedown. propagation on siblings event.targets
(3 answers)
Closed 3 years ago.
fiddle
when I click on the respective "Click me!" buttons only one console.log (click event) is fired.
Why is the propagation stopped here? The click should to through "Click me!" into the button underneeth with the ".......".
<button style="position: absolute" onclick="onClick()">Click me!</button>
<button style="height: 4em" onclick="onClick()">...............................................</button>
On the web everyone whats to stopPropagation, but I want to enforce it!
The desired outcome is to get two events firing from a single click.
Events propagate to parent elements, not sibling elements, so event propagation won't help you here. (The fact that you've changed the layout to make it look like the buttons are nested doesn't affect this.) If you need to trigger both events you'll need to do it manually, or actually nest the elements (changing the parent to something other than a <button>, because they can't be nested):
onClick = function() {
console.log("onclick triggered");
}
<div style="height: 4em; border: 1px solid;" onclick="onClick()">
<button onclick="onClick()">Click me!</button>
...............................................
</div>
This question already has answers here:
Stop mouse event propagation
(15 answers)
Closed 5 years ago.
I have a list of elements.
Each element have a global click event and a child icon which should also be clickable.
Example :
<div class="parent-element" (click)="clickParent()">
<span class="button" (click)="childClick()">
</span>
</div>
For now when I click on the span, both click are fired. How can I separate it from the parent click event ?
Catch the event on both methods (parent and child) and call event.preventDefault() or event.stopPropagation()
This question already has answers here:
Any Javascript event occurring when user clicks Stop load button?
(3 answers)
Closed 6 years ago.
Say I have an HTML form on a page with a submit button. I've attached some logic to the form submit event to change the display to show a loading icon (in addition to posting the form to the server). However, if I press escape or click the browser's stop button, the submission event will stop but right now, the loading icon won't go away.
Is it possible to attach some JS logic to the "form interrupt" event to then clear the loading icon?
window.onabort looks as though it may have been designed for this purpose, but it doesn't appear to have any browser support.
Solutions which use either JQuery or plain vanilla JS are fine.
Edit to with clarifying code example
<form action="/myServerRoute" method="post">
<input type="submit" value="Submit form" onclick="DisplayLoadingAnimation();" />
</form>
<script>
function DisplayLoadingAnimation() {
alert('Imagine this manipulates the DOM to render loading elements.');
}
function HideLoadingAnimation() {
alert('Imagine this manipulates the DOM to clear loading elements.');
}
</script>
Imagine I click the "Submit form" button but then press the browser's stop button before the server can respond. How can I make it so that HideLoadingAnimation is executed when this happens?
The basic structure for something like this is. When button is clicked, register a listener for keypress on the document ( make sure to remove it later if it is not needed ). The keypress event can check for which key is pressed ( you can add the code ) and then modify the html appropriately. Then if the key is pressed the listener should be removed.
There isn't a cross browser way to determine the stop button was clicked. IE has onstop event though, webkit doesn't support this.
function mySubmit( event ){
event.preventDefault();
document.querySelector('button').innerHTML = "Loading";
listenForEscapeAndStop();
}
function listenForEscapeAndStop(){
var reference = document.addEventListener("keyup", function(){
document.querySelector('button').innerHTML = "My Button";
document.removeEventListener("keyup", reference);
});
document.onstop = function(){ // IE only
document.querySelector('button').innerHTML = "My Button";
};
}
<button onclick='mySubmit( event )'> My Button </button>
This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 8 years ago.
I am having a strange issue.
I have created a code snippet where the user can create dynamic input elements and remove them as well
When I click the remove class then it's not triggering the onclick event.
<input type='button' value='Add Button' id='addButton'>
<div id="TextBoxesGroup"></div>
I have created a jsFiddle
http://jsfiddle.net/squidraj/b4Cs8/2/
Use event delegation in jquery
$(document).on("click" , "a.removeField", function (event) {
event.preventDefault();
$(this).parent().remove();
counter--;
});
Fiddle