I'm trying to find a very simple way to have users click a picture that links to an order form and will autofill the item number.
Recommended Reading: Decoupling Your HTML, CSS, and JavaScript.
I'm going to assume the order form is on the same page as the picture (since you haven't specified).
let work on getting click
to work using jQuery. Other things like src
with be excluded for brevity.
<img class="js-formfill-item" />
<img class="js-formfill-item" />
<img class="js-formfill-item" />
<div><input type="text" name="ItemNumber"></div>
Now we have a way to target one or more items that, as the class describes, can fill in a form. Next we wire-up jQuery to wait for the entire dom to be ready and wire up a click event.
$(document).ready(() => {
$('.js-formfill-item').on('click', (event) => {
});
});
I would highly recommend not using the alt tag to store data as it does have it's own purpose that is not related to Item Number.
I prefer to lousy-couple my jQuery as possible, so to target an element we simply add a user-defined data attribute I prefer to name the attribute target
to target the input element. Additionally I'd use another data attribute it store it's item number.
$(document).ready(() => {
$('.js-formfill-item').on('click', (e) => {
// e.currentTarget will be the html element clicked that has the class .js-formfill-item
// we wrap it with $() to use it with jQuery
var $this = $(e.currentTarget);
var target = $this.data('target');
var itemid = $this.data('itemid');
$(target).val(itemid);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img class="js-formfill-item"
data-target=".js-formfill-input"
data-itemid="1"
src="https://via.placeholder.com/50"/>
<img class="js-formfill-item"
data-target=".js-formfill-input"
data-itemid="2"
src="https://via.placeholder.com/50"/>
<img class="js-formfill-item"
data-target=".js-formfill-input"
data-itemid="3"
src="https://via.placeholder.com/50"/>
<div>
<input class="js-formfill-input" type="text" name="ItemNumber">
</div>