One Button Firing Another Buttons Click Event
I'd like two submit buttons on a form i have my team building, one above the fold, and one below. I'm getting complaints from my tech team about adding it because it requires some
Solution 1:
I'm only familiar with ASP.net and C# buttons, but using C# you could wire two different buttons to the same click event handler. You could also do it client side by triggering the primary buttons click event with your secondary button. Here's a VERY simple example:
HTML
<inputtype="button"id="primaryButton" onclick="ExistingLogic()" />
<inputtype="button"id="secondaryButton"
onclick="document.getElementById('primaryButton').click()" />
Solution 2:
<inputtype="button" id="primaryButton" onclick="ExistingLogic()" />
<inputtype="button" id="secondaryButton"/>
$('#secondaryButton').click(function(){
$("#primaryButton").click();
})
Solution 3:
If you want to use vanillaJS to do this... here is a generic very long way (with functions for both to be clear what is happening).
html
<inputtype="button"id="primaryButton" />
<inputtype="button"id="secondaryButton"/>
script
const primary = document.getElementById('primaryButton');
const secondary = document.getElementById('secondaryButton');
functionsomePrimaryAction(e){
e.preventDefault();
console.log('you clicked the primary button');
}
functionclickPrimaryButton(e){
e.preventDefault();
console.log('you clicked the secondary button');
primary.click();
}
primary.addEventListener("click", somePrimaryAction, false);
secondary.addEventListener("click", clickPrimaryButton, false);
Solution 4:
Yeezy
<button onclick="$('#button2').click()">button 1</button>
<button id="button2" onclick="doSomethingWhenClick()">button 2</button>
(((You need jQuery to run this)))
Post a Comment for "One Button Firing Another Buttons Click Event"