We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,602 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

how to increment the input field value in javascript on page load??

how to increment the input field value in javascript on page load??
for example

<input type="text" name="itemquantity[]" value="1"/>

after page load it should be like

<input type="text" name="itemquantity[]" value="2"/>

and so on on next page loads
any help???

4
Contributors
7
Replies
4 Hours
Discussion Span
3 Months Ago
Last Updated
8
Views
mehar89
Newbie Poster
4 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Try this

<!DOCTYPE HTML>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>

</head>
<body>
<?php
session_start();
if(isset($_SESSION['pageload'])) {
    $_SESSION['pageload']=$_SESSION['pageload'] + 1;
}
else {
    $_SESSION['pageload']=1;
}


?>
<div class="fieldClass">

</div>
<script type="text/javascript">
$(function(){
          var count ='<?php echo $_SESSION['pageload'];  ?>';
         for (var i = 1; i <=count; i++) {
             $('.fieldClass').append('<input type="text" name="itemquantity[]" value="'+i+'"/><br />');
         }
});
</script>

</body>
</html>

And Unset session $_SESSION['pageload'] if you go to another page

Bachov Varghese
Junior Poster
123 posts since Sep 2011
Reputation Points: 41
Solved Threads: 29
Skill Endorsements: 3

@Bachov i need code only in javascript i'm not wanted to use sessions

mehar89
Newbie Poster
4 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
urtrivedi
Posting Virtuoso
1,719 posts since Dec 2008
Reputation Points: 299
Solved Threads: 362
Skill Endorsements: 24

Hi Mehar89

here you go, let me know if you have questions.

var form = document.getElementById("myForm"),
    incInput = form["formitemquantity[]"],
    len = incInput.length,
    ctr = 0;

for(;ctr < len; ctr++){
    incInput[ctr].value = ctr;
}
gon1387
Posting Whiz in Training
233 posts since Jan 2011
Reputation Points: 32
Solved Threads: 37
Skill Endorsements: 3

@gon1387 it is not working???

mehar89
Newbie Poster
4 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

when page reloads, everything in javascript resets, So I advised you to store last number in cookie and increment and display that cookie value on every page load

http://www.w3schools.com/js/js_cookies.asp

urtrivedi
Posting Virtuoso
1,719 posts since Dec 2008
Reputation Points: 299
Solved Threads: 362
Skill Endorsements: 24

have you run it when the dom's ready?

Here's an online example in JSFiddle: JSFiddle Example
I've set the onDomReady on js fiddle. Here's a sample on firing the snippet when the page finishes loading. There are many flavors on firing this up, like checking the document readystate and so on and so for. But hopefully, this explains it.

window.onload = function(){
    var form = document.getElementById("myForm"),
        incInput = form["formitemquantity[]"],
        len = incInput.length,
        ctr = 0;
    for(;ctr < len; ctr++){
        incInput[ctr].value = ctr;
    }
};
gon1387
Posting Whiz in Training
233 posts since Jan 2011
Reputation Points: 32
Solved Threads: 37
Skill Endorsements: 3

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0790 seconds using 2.71MB