1

This is code that I use, can someone tell me how to make that input have min and max value. I tried the html attribute max="10" but it's not working. I want to set min value to be 0 and max 10. And when you click on plus you can't add more point when field free points reach 0 points.

<input type="button" id="minus" value="-" onclick="textb.value = (textb.value-1);textc.value = (+textc.value+1)  ">
<input type="text" id="textb" name="name" value="0" readonly="readonly" />
<input type="button" value="+" onClick="textb.value = (+textb.value+1);textc.value = (textc.value-1)">Health
<p>
    <input type="button" id="minus" value="-" onclick="textm.value = (textm.value-1);textc.value = (+textc.value+1)  ">
    <input type="text" id="textm" name="name" value="0" readonly="readonly" />
    <input type="button" value="+" onClick="textm.value = (+textm.value+1);textc.value = (textc.value-1)">Energy</p>
<p>
    <input type="button" id="minus" value="-" onclick="textd.value = (textd.value-1);textc.value = (+textc.value+1)  ">
    <input type="text" id="textd" name="name" value="0" readonly="readonly" />
    <input type="button" value="+" onClick="textd.value = (+textd.value+1);textc.value = (textc.value-1)">Chakra</p>
<p>
    <input type="text" id="textc" name="name" readonly="readonly" value="10" />Free points</p>

Fiddle

4
  • 3
    max and min only apply to input type="number" Commented Jul 17, 2014 at 17:56
  • Using inline JavaScript in this manner is a bad idea. Commented Jul 17, 2014 at 17:57
  • 1
    @Valerij Also apply to input type="range" Commented Jul 17, 2014 at 17:57
  • Your javascript is also completely disregarding the maximum and minimum values; if you want it to not go over/under, you need to do your own validation in the javascript. Commented Jul 17, 2014 at 17:59

2 Answers 2

1

Here is a different approach at the problem. I'm not sure if you're allowed to have negative values in the three properties (if not you'll have to add more logic).

I've eliminated the inline JavaScript and added some accessibility logic to the HTML code which the JavaScript hooks into in order to find the right element.

See jsFiddle

HTML

<p>
    <button type=button value=-1 aria-controls=textb>-</button>
    <input type=number id=textb readonly value=0 min=0 max=10 name=name>
    <button type=button value=1 aria-controls=textb>+</button>
    <label for=textb>Health</label>
</p>
<p>
    <button type=button value=-1 aria-controls=textm>-</button>
    <input type=number id=textm readonly value=0 min=0 max=10 name=name>
    <button type=button value=1 aria-controls=textm>+</button>
    <label for=textm>Energy</label>
</p>
<p>
    <button type=button value=-1 aria-controls=textd>-</button>
    <input type=number id=textd readonly value=0 min=0 max=10 name=name>
    <button type=button value=1 aria-controls=textd>+</button>
    <label for=textd>Chakra</label>
</p>
<p>
    <label for=textc><input type=number value=10 readonly id=textc /> Free points</label>
</p>

JavaScript

var buttons = document.querySelectorAll('button[aria-controls]'),
    i;

var freePointsEl = document.getElementById('textc');
var freePoints = Number(freePointsEl.value);
var maxFreePoints = 10; // this could be abstract to use the max attribute of the input element if you wish

for (i = 0; i < buttons.length; ++i) {
    buttons[i].addEventListener('click', function (event) {
        var input = document.getElementById(this.getAttribute('aria-controls'));

        freePoints -= Number(this.value);

        if (freePoints < 0) {
            // no more free points
            // do not do anything
            freePoints = 0;
        } else if (freePoints > maxFreePoints) {
            // cannot exceed max free points when subtracting
            // do not do anything
            freePoints = maxFreePoints;
        } else {
            // we're ok, do something
            input.value = Number(input.value) + Number(this.value);
        }

        freePointsEl.value = freePoints;

    }, false);
}
Sign up to request clarification or add additional context in comments.

Comments

0

apart from the functionality missing there are numerous issues with your code. I tried my best to address them all and give you easy understandable code.

So first of all: inline javascript ( i.e. putting code in onchange) is rather bad practique
second you have multiple elements with the same id(<button id="minus") one should use class in such cases.

i modified your html as following

<p>
    <input type="button" class="minus" value="-">
    <input type="text" id="textb" value="0" readonly="readonly"/>
    <input type="button" class="plus" value="+">
    Health
</p>
<p>
    <input type="button" class="minus" value="-">
    <input type="text" id="textm" value="0" readonly="readonly" />
    <input type="button" class="plus" value="+">
    Energy
</p>
<p>
    <input type="button" class="minus" value="-">
    <input type="text" id="textd" value="0" readonly="readonly"/>
    <input type="button" class="plus" value="+">
    Chakra
</p>
<p>
    <input type="text" id="textc" readonly="readonly" />
    Free points
</p>

and this js, note i used jQuery to save some typing, but with minor modifications this code can be used with pure vanilla DOM API (document.querySelectorAll and so on)

$(function(){
    var free = 10
      , max  = 10
      , min  = 0
      ;
    $('.minus').click(function(){
        var edit = $(this).parent().find('[type="text"]').get(0)
        if(edit.value > 0 && edit.value > min) {
            edit.value--;
            free++;
        }
        $("#textc").get(0).value = free;
    });
    $('.plus').click(function(){
        var edit = $(this).parent().find('[type="text"]').get(0)
        if(free > 0 && edit.value < max) {
            free--;
            edit.value++;
        }
        $("#textc").get(0).value = free;
    });
    $("#textc").get(0).value = free;
});

and here is it in the fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.