I am a rookie web developer with a website containing 3 different items, each with its individual "contact me" button. When the button is pressed for the first time, the page scrolls down to display the form.
My problem is the event fires once pr button, instead of once in total. I want the script to fire once regardless of which button is pressed, then the script should delete itself or stop further executions.
I just don't see where this script fails. Please enlighten me :)
Below is a very simplified proof of concept with a jsfiddle attached.
https://jsfiddle.net/8wegb32s/
#megaspacer {
height: 1000px;
}
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<input form="form" class="orderbutton" type="radio" id="1" name="product" value="Value1" />
<input form="form" class="orderbutton" type="radio" id="2" name="product" value="Value2" />
<input form="form" class="orderbutton" type="radio" id="3" name="product" value="Value3" />
<div id="megaspacer">
<!--Give me som space -->
</div>
<form id="anchor">
<input type="text" name="name" id="name" class="form-control text-dark" placeholder="Name*">
</form>
<script id="runonce">
$(".orderbutton").one("click", function() {
$('html, body').animate({
scrollTop: $("#anchor").offset().top
}, 2000);
$("script").remove("#runonce");
});
</script>
Solved in https://jsfiddle.net/gaby/g7bw1mkn/10/