Complete the code to prevent the default form submission behavior.
function MyForm() {
function handleSubmit(event) {
event.[1]();
alert('Form submitted!');
}
return (
<form onSubmit={handleSubmit}>
<button type="submit">Submit</button>
</form>
);
}Calling event.preventDefault() stops the browser from performing the default action, like submitting a form and reloading the page.
Complete the code to prevent the default link navigation when clicking the anchor tag.
function Link() {
function handleClick(event) {
event.[1]();
alert('Link clicked without navigation');
}
return <a href="https://example.com" onClick={handleClick}>Click me</a>;
}Using event.preventDefault() stops the browser from following the link's URL.
Fix the error in the event handler to correctly prevent default behavior.
function Button() {
function handleClick(event) {
event.[1]();
alert('Button clicked');
}
return <button onClick={handleClick}>Click me</button>;
}You must call event.preventDefault() as a function with parentheses to prevent the default action.
Fill both blanks to prevent default behavior and stop event bubbling in the click handler.
function CustomButton() {
function handleClick(event) {
event.[1]();
event.[2]();
alert('Clicked without default or bubbling');
}
return <button onClick={handleClick}>Click me</button>;
}Calling preventDefault() stops the default action, and stopPropagation() stops the event from bubbling up to parent elements.
Fill all three blanks to prevent default, stop propagation, and log the event type.
function LoggerButton() {
function handleClick(event) {
event.[1]();
event.[2]();
console.log(event.[3]);
}
return <button onClick={handleClick}>Click me</button>;
}This code prevents the default action, stops the event from bubbling, and logs the event type (like 'click').