Using buttons as spinners for input elements with identical styles but unique identifiers

Currently, I am in the process of developing a project that involves users. In order to display all users, I am executing a query on an SQL database. After styling the interface, I have added an input element beside each user, which initializes at zero. Additionally, there are two buttons - one for incrementing (#up) and another for decrementing (#down) the value displayed in the input element. Essentially, these buttons act as spinners. Moreover, I aim to set limits for the input element, with a minimum value of 5 and a maximum of 500.

Despite my attempts at implementing various 'onclick' functions, I have not been successful. While using 'GetElementById', it only alters the value of the input element for the first user due to the unique ID assigned to it. How can I rectify this issue?

I deeply appreciate any assistance provided. As a beginner, I welcome corrections if I have made any mistakes rather than receiving downvotes.

Below is the HTML code:

 <span class='username'>" . $row['username'] . "</span>
<form name='matchcreator' id='amount' action='arena.php' method='post'>
<input name='m-maker' type='text' id='price' maxlength='15' value='0'/>
<button id='up' type='button'><img src='images/up.png' width='10px' height='10px' href='#'></button>
<button id='down' type='button'><img src='images/down.png' width='10px' height='10px' href='#'></button>
</form>

Answer №1

You have two options to customize the input controls: either follow David Cash's advice and utilize the HTML5 input element with spinners, or you can opt to eliminate the IDs from the up/down arrows and the input element itself. Here is an example of what that might look like:

Customized HTML

    <span class='username'>".$row['username']."</span>
    <form name='matchcreator' id='amount' action='arena.php' method='post'>
        <input name='m-maker' type='text' class='price' maxlength='15' value='0'/>
        <button class='up' type='button'><img src='images/up.png' width='10px' height='10px' href='#'></button>
        <button class='down' type='button'><img src='images/down.png' width='10px' height='10px' href='#'></button>
    </form>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script>
    $(function () {
    $(".up").on("click", function () {
        var trigger = $(this);
        var input = trigger.prev();
        if (Number(input.val()) < 500) {
            input.val(Number(input.val()) + 1);
        }
        else { alert("max value reached"); }
    }); 
    $(".down").on("click", function () {
        var trigger = $(this);
        var input = trigger.prev().prev();
        if (Number(input.val()) > 5) {
            input.val(Number(input.val()) - 1);
        }
        else { alert("min value reached"); }
    });
    }
</script>

Answer №2

Why not give simple HTML 5 a try for your input box?

<input name ="m-maker" name="price" type="number" min="1" max="500" value="5">

By using this code, you can easily create an input box with spinners without the need for additional buttons to adjust the value.

Check out this example on Plunker

Answer №3

Here is a demonstration of the requested functionality using classes instead of IDs:

<form name='matchcreator' class='amount' action='arena.php' method ='post'>
    <input  name='m-maker' type='text' class='price' maxlength='15' value='0'/>
    <button class='up' type='button'><img src='images/up.png' width='10px' height='10px' href='#'></button>
    <button class='down' type='button' '><img src='images/down.png' width='10px' height='10px' href='#'></button>
</form>

Sample usage of jQuery:

$(".down").on("click", function() {
    var input = $(this).prev(".price");
    input.val(parseInt(input.val()) - 1);
});

$(".up").on("click", function() {
    var input = $(this).prev(".price");
    input.val(parseInt(input.val()) + 1);
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Performing a multitude of if statements simultaneously consistently results in an undefined outcome after the sixth iteration

Oops! I can't believe I forgot my original question! Every time I run the sixth if statement in my code, I encounter a typeError Undefined. Interestingly, when I switch the positions of the fifth and sixth statements, the if(!data.body.main) condition ...

The getElementById function can only select one option at a time and cannot select multiple options

I'm having an issue with a JavaScript function that is supposed to allow me to select multiple options, but it's only selecting the first one. Here is the JavaScript code: function index(){ var a="${staffindex}".replace("[",""); ...

Is there a way to send a JSON object and a file to an ASP.NET server using the fetch method?

I'm facing a challenge here, as I attempt to send a json object from my indexedDb along with an IFormFile object to the server at the same time. The method that handles this scenario is structured like so: [HttpPost] public async Task<IActionR ...

How can I set the text to be in the center of a VML

For my email template, I need to center text over an image. Although I attempted a solution from this source, the image is not being displayed. Below is the code snippet: <div align="center" style="margin: 0; padding: 0;"> <table border="0" c ...

Calculating the computed width based on flex-basis at 0% and the flex-grow factor: what's the deal

Below is an example of HTML with default flex properties being used: <div class="container"> <div class="box"> BOX 1</div> <div class="box"> BOX 2</div> <div class="box box2"> BOX 3 .</div> </div> The ...

The css values for component _nghost-c0 in an Angular application

Recently, I've been delving into Angular 5 and couldn't help but notice the peculiar html tags with ng generated attributes like _nghost-c0 and _nghost-c1... This got me wondering, what exactly do these attributes signify? [_nghost-c3] .employee ...

Discover the process of integrating PWA features into an Express web application

I'm currently in the process of integrating PWA into a web application. I've created a manifest.json file and added it to the head of my page, which is loading correctly. However, the service worker registered in my app.js doesn't seem to be ...

``Troubleshooting: Problem with Rails checkboxes and error-ridden fields

Hello there! I've encountered an interesting problem that I've traced back to using a custom bootstrap checkbox. Everything works fine, except for when I try to submit the form without checking the checkbox and receive an error message. After thi ...

What is the best way to insert identical text into several lines at once using emacs?

My HTML file contains numerous links to a database, but the URLs are formatted as: <a href=/bw/examplefile.html> Although I attempted to use wget with the --base parameter to download all the links, it didn't work as expected. Instead of trou ...

Issues with login validation in HTML when utilizing JSON and PHP

While creating a login form in HTML using JSON and PHP, I encountered an issue where the if statements in the success function were not working properly. However, the beforeSend and error functions are functioning as expected. Can someone assist me in iden ...

Navigating external pages with Vue Router

Could really use some assistance. I've got a JSON file filled with various URL links, some internal and some external. This is what the JSON structure looks like: [ {stuff..., "Url":"https://www.google.com/", stuff..}, {stuff... ...

The filter is displaying incorrect categories

I am facing an issue with creating a work filter based on the last column which represents categories. When I select an option from the dropdown, I want to display only that specific category and hide the others. Currently, when I try clicking on an option ...

What should I do to resolve the issue of ajax failing to update data randomly?

This script is designed to take the value entered into an HTML form and send it to the ../Resources/BugReport.php file. The data is then inserted into a database in that file, and the table in the ../Resources/BugDisplay.php file displays the information f ...

"Integrating Vuetify into a single-spa Vue application: Step-by-step

vue 2.6.14 vuetify 2.6.9 What is the process for integrating vuetify into a vue application? I am attempting to import my project into another project as a microfrontend application, but encountering issues. Here are the steps I followed: Create-single- ...

Enhancing your Vue web application with Bootstrap: A guide to customization

Utilizing bootstrap-4 within my Vue web application has been challenging. I am unable to customize it as explained here. My index.html utilizes Bootstrap CDN, as shown below: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

What is the method for configuring the asp-for input tag helper to create camelCase names?

My view model is structured as follows: public class MyModel{ public DateTime? StartDate {get;set;} } When using an input tag on a view with the asp-for tag helper, the default HTML generated looks like this: <input type="datetime" id="StartD ...

Inserting information into a MySQL database using a dynamic jQuery-generated form

i'm attempting to input data into MySQL from a dynamic form utilizing jQuery. Just to clarify, I am working with Twitter Bootstrap. Currently, I managed to insert "rows" but without any data. The most helpful resources I came across are located here f ...

What is the sequence in which the browser handles CSS?

I have a specific class that is being styled in multiple places on my website. I am curious about the order in which the browser reads and applies these different styles. Can you explain this process to me? Inline Style <div class="yellowtag" sty ...

Issue with Node's jsonwebtoken library: jwt.sign() method fails to include payload in generated token

I am currently working on developing an API with Node.js and have configured const jwt = require('jsonwebtoken') to handle JWTs. However, I am facing an issue where the generated token does not contain the payload information. To troubleshoot thi ...

Employing negative margin to connect div1 to the left side of div2

Is there a way to attach a dismiss button to the left of a contact in a sidebar? I am trying to achieve a layout similar to the one shown in this image: I attempted using margin-left: -50px; or left: -50px;, but the dismiss div seems to disappear. Even th ...