2020-10-27

How can I make onClick apply to the label of a number form?

I have a page where with about 100 number forms. Each form has a different label, name, id, etc. This is to apply some other javascript later on.

Two questions: Is it possible to rig the javascript so the number form increases by one when clicking on the label (ie. click on "Apples") rather than the number box? And since every form has a different id, is it possible to grab each form by something other than its id (an alternate catch-all so it can easily be applied to around 100 different forms)?

Thank you in advance!

<html lang="en">
<head>
<script>
    var i = 0;
    function onClick() {
        document.getElementById('applesQuantity').value = ++i;
    }
</script>
</head>
<body>
</body>

<label>Apples </label><input type="number" name="apples-quantity" id="applesQuantity" onclick="onClick()">
<label>Oranges </label><input type="number" name="oranges" id="orangesQuantity" onclick="onClick()">

</html>


from Recent Questions - Stack Overflow https://ift.tt/37GClza
https://ift.tt/eA8V8J

No comments:

Post a Comment