Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to increment part of a string one by one on clicking a button?

    I am using string like AAES001. I want to increment part of the string like 001 to 002 one by one. As i click the submit button it increments the string automatically. The string should not be repeated it only increments serially.
    I am storing the string in a hidden field called AAESNo.

    I have done this:

    Code:
    var val="AAES001";
    
    
    function increment()
    {
    		document.getElementById('AAESNo').value=val;
    		var val1= val.substring(4, 7);
            val1++;
    		var val2= val.substring(0, 4);
    	        val= val2+"00"+val1;
    	        alert(val);
            
    }
    Last edited by cyborg911; 08-07-2013 at 01:33 PM.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,760
    Thanks
    23
    Thanked 546 Times in 545 Posts
    And your question is what?
    Evolution - The non-random survival of random variants.

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Your code seems to work
    as you described if triggered
    from a button .
    If you do a form submission to the same page
    the page will be reset and val
    will again equal AAES001.
    You will need to preserve the
    incremented value through server
    side programming, cookies, local storage,
    or in the query string.

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,760
    Thanks
    23
    Thanked 546 Times in 545 Posts
    Your submit should by done via javascript and That should call the increment function.
    And you should change that to something like this:
    Code:
    <input type="hidden" id="AAESNo" value="AAES001">
    <input type="button" onclick="increment();" value="PUSH">
    
    <script type="text/javascript">
    function increment(){
    	val = document.getElementById('AAESNo').value;
    	var val1= val.substring(4, 7);
    	val1++;
    	var val2= val.substring(0, 4);
    	val= val2+"00"+val1;
    	document.getElementById('AAESNo').value = val;
    	alert(document.getElementById('AAESNo').value);  //  ONLY HERE TO SHOW THE INCREASE
    }
    </script>
    Evolution - The non-random survival of random variants.

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    If you want this value to be
    incremented by all your visitors,
    and not start over at AAES001
    for each new visitor then it
    must be done server side.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •