Jump to content

Recommended Posts

Hi,

I have code below, a text input box with four increment / decrement buttons. Two for change the value by one step, two for change the value by 10. Three of them are working fine, but the +10 button changes the value to 1010, not 20 when the starting value is 10.

I don't get it.. Why -10 works and not the +10... rows are almost identical..

 

<INPUT TYPE="TEXT" NAME="quantity" SIZE="5" MAXLENGTH="6" VALUE="10" CLASS="norm" id="quantity" onkeypress='return event.keyCode!=13'>
<input type="button" value="-" onmouseover="this.style.border='1px solid '; this.style.padding='0px';" onmouseout="this.style.border='0px'; this.style.padding='1px';" onclick='var d=document.getElementById("quantity"); if (d.value>-99999) { d.value--; } else { d.value=-99999;};'>
<input type="button" value="+" onmouseover="this.style.border='1px solid '; this.style.padding='0px';" onmouseout="this.style.border='0px'; this.style.padding='1px';" onclick='var d=document.getElementById("quantity"); if (d.value<999999) { d.value++; } else { d.value=999999;};'>
<input type="button" value="-10" onmouseover="this.style.border='1px solid '; this.style.padding='0px';" onmouseout="this.style.border='0px'; this.style.padding='1px';" onclick='var d=document.getElementById("quantity"); if (d.value>-99990) { d.value=d.value-10; } else { d.value=d.value;};'>
<input type="button" value="+10" onmouseover="this.style.border='1px solid '; this.style.padding='0px';" onmouseout="this.style.border='0px'; this.style.padding='1px';" onclick='var d=document.getElementById("quantity"); if (d.value<999990) { d.value=d.value+10; } else { d.value=d.value;};'>
<FORM ACTION="form.php" METHOD="POST" style="display: inline;" ID="formi" name="formi">
</FORM>

Link to comment
https://forums.phpfreaks.com/topic/269931-increment-value-problem-with-a-button/
Share on other sites

Probably having to do with the way the string is parsed. You may have to parse the string in order to do it, but there is a much better way than this.

 

I used jQuery because it's most often shorter than plain JS by a long shot, but I can do an example in plain JS if you wish.

Example: http://xaotique.no-ip.org/tmp/11/

 

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   <head>
       <title>Xaotique</title>
       <meta http-equiv="content-type" content="text/html;charset=utf-8" />
       <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
       <script type="text/javascript" src="script.js"></script>
   </head>

   <body>
       <input type="text" value="0" id="result" />
       <input type="button" class="modify" value="-10" />
       <input type="button" class="modify" value="-1" />
       <input type="button" class="modify" value="1" />
       <input type="button" class="modify" value="10" />
   </body>
</html>

 

script.js

$(document).ready(function()
{
   $(".modify").click(function()
   {
       $("#result").val(parseInt($("#result").val()) + parseInt($(this).val()));
   });
});

Edited by Xaotique

Thanks for the example. And you were right, it was about parsing the string. Still don't understand why -10 was ok and +10 was not..

Anyway, here's my new code:

 

<input type="button" value="-" onclick='var d=document.getElementById("quantity").value; if (d>-99999) { document.getElementById("quantity").value=parseInt(d)-1;};'>
<input type="button" value="+" onclick='var d=document.getElementById("quantity").value; if (d<999999) { document.getElementById("quantity").value=parseInt(d)+1};'>
<input type="button" value="-10" onclick='var d=document.getElementById("quantity").value; if (d>-99990) { document.getElementById("quantity").value=parseInt(d)-10;};'>
<input type="button" value="+10" onclick='var d=document.getElementById("quantity").value; if (d<999990) { document.getElementById("quantity").value=parseInt(d)+10;};'>

 

Need to use javascript because there's more related boring 'stuff' in which I don't have the time/interest to change ;)

 

So thanks again for the help!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.